I have this Provider in my code. its suppose to return list
class ServingProvider extends ChangeNotifier { List<Serving> servings = []; getServings() { Firestore.instance .collection('servings') .getDocuments() .then((value) => value.documents) .then((value) => value.map((serving) => Serving.fromJson(serving.data)).toList()) .then((value) => servings.addAll(value)) .then((value) => notifyListeners()); print(servings); } // List<Serving> get servings => servings; }
I want to get the value here but I'm getting null for some reason.
class KMultiScrollWheel extends StatelessWidget { final String title; final String value; final BuildContext passedContext; final String dialogTitle; KMultiScrollWheel({ @required this.title, this.value, @required this.passedContext, this.dialogTitle, }); @override Widget build(BuildContext context) { final servings = Provider.of<ServingProvider>(context).servings; print(servings)
This is the wrapper of my class.
ChangeNotifierProvider( create: (_) => ServingProvider(), child: KMultiScrollWheel( title: 'Serving Size', passedContext: context, dialogTitle: 'Select Serving', ), ),
I'm coming from react and I'm completely lost for something that should be rather simple. thanks in advance.
One last question whats the point of using get method when I can access the servings already declared at the top.
https://stackoverflow.com/questions/65819826/flutter-how-to-get-the-value-of-a-provider-out January 21, 2021 at 09:06AM
没有评论:
发表评论