Last active
January 7, 2020 21:43
-
-
Save azamsharp/f4bce395d89bd0859d209bac74b0090e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:stocks_app_flutter/models/stock.dart'; | |
import 'package:stocks_app_flutter/services/webservice.dart'; | |
class StockListViewModel extends ChangeNotifier { | |
List<StockViewModel> stocks = List<StockViewModel>(); | |
List<StockViewModel> allStocks = List<StockViewModel>(); | |
Future<void> fetchStocks() async { | |
final result = await Webservice().getStocks(); | |
stocks = result.map((stock) => StockViewModel(stock: stock)).toList(); | |
allStocks = stocks; | |
notifyListeners(); | |
} | |
void search(String searchTerm) { | |
if(searchTerm.isEmpty) { | |
stocks = allStocks; | |
} else { | |
stocks = allStocks.where((stock) => stock.symbol.startsWith(searchTerm)).toList(); | |
} | |
notifyListeners(); | |
} | |
} | |
class StockViewModel { | |
final Stock stock; | |
StockViewModel({this.stock}); | |
String get symbol { | |
return stock.symbol.toUpperCase(); | |
} | |
String get company { | |
return stock.company; | |
} | |
double get price { | |
return stock.price; | |
} | |
String get change { | |
return stock.change; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment