我有一个名为Database.dart的文件,在其中插入了“虚拟”数据并建立了与Firebase的连接。使用Meals.dart我想稍后在屏幕上显示数据。现在,我想在调试控制台中显示此虚拟数据。我试图更改Meals.dart中的“ mealList”,但没有弄清楚代码中的问题是什么。
这是我得到的错误代码。
════════ Exception caught by widgets library ═══════════════════════════════════
The following NoSuchMethodError was thrown building Meals(dirty, dependencies: [_InheritedProviderScope<List<MealsAndWhen>>], state: _MealsState#ed4e6):
The method 'forEach' was called on null.
Receiver: null
Tried calling: forEach(Closure: (MealsAndWhen) => Null)
The relevant error-causing widget was
Meals
lib/…/MealPlan/mealplan.dart:23
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 _MealsState.build
package:mealapp/…/Widgets/meals.dart:17
#2 StatefulElement.build
package:flutter/…/widgets/framework.dart:4619
#3 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4502
#4 StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:4675
...
════════════════════════════════════════════════════════════════════════════════
flutter:
flutter:
我的Database.dart代码为
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:mealapp/models/Widgets/whenAndWhatToEat.dart';
class DatabaseService {
final String uid;
DatabaseService({ this.uid });
// collection reference
final CollectionReference mealCollection = Firestore.instance.collection('mealList');
Future updateUserData(String shopping, String name, String whatEat, String whenEat) async {
return await mealCollection.document(uid).setData({
'shopping': shopping,
'name': name,
'whatEat': whatEat,
'whenEat': whenEat,
});
}
//Meal List from Snapshot
List<MealsAndWhen> _mealAndWhenListFromSnapshot(QuerySnapshot snapshot){
return snapshot.documents.map((doc){
return MealsAndWhen(
whenEat: doc.data['When'] ?? '',
whatEat: doc.data['What'] ?? '',
);
}).toList();
}
//get shoppingList Stream
Stream<List<MealsAndWhen>> get mealList {
return mealCollection.snapshots()
.map(_mealAndWhenListFromSnapshot);
}
}
我的Meals.dart代码为
import 'package:flutter/material.dart';
import 'package:mealapp/models/Widgets/whenAndWhatToEat.dart';
import 'package:provider/provider.dart';
class Meals extends StatefulWidget {
@override
_MealsState createState() => _MealsState();
}
class _MealsState extends State<Meals> {
@override
Widget build(BuildContext context) {
final meals = Provider.of<List<MealsAndWhen>>(context);
meals.forEach((meals){
print(meals.whatEat);
print(meals.whenEat);
});
return Container(
);
}
}
答案 0 :(得分:1)
将forEach包装为空。 因为从Firebase答复需要一些时间。
“ forEach”方法在null上被调用。
if (meals != null) {
meals.forEach((meals){
print(meals.whatEat);
print(meals.whenEat);
});
}
在返回区域执行以下操作:
return (meals == null || meals.isEmpty) ? Text('empty') : ListView.builder(
itemCount: meals.length,
itemBuilder: (context, index) {
print(meals[index].whatEat);
print(meals[index].whenEat);
return text('Something');
},
);
答案 1 :(得分:0)
您错误地调用了属性。
您可以定义属性'whatEat': whatEat
和'whenEat': whenEat
。
然后,您使用不同的名称['When']
和['What']
调用属性。
解决方案:
尝试将['When']
更改为['whenEat']
,将['What']
更改为['whatEat']