![](/img/trans.png)
[英]The getter 'displayName' was called on null. - Flutter
[英]The getter 'iterator' was called on null. Flutter & FireBase
i have a problem and dont know how to solve... I want to get data through a stream from Firebase i have UserData in FireBase and now want to get in another script by using a Stream of this UserData(Cutom Class) but the stream正在抛出错误。 如果我在 null 上使用迭代器,我已经证明了基本行。 但我想我没有。 供应商一定有问题。 这是错误:
════════ Exception caught by provider ══════════════════════════════════════════════════════════════
The following assertion was thrown:
An exception was throw by _MapStream<DocumentSnapshot, UserData> listened by
StreamProvider<UserData>, but no `catchError` was provided.
Exception:
NoSuchMethodError: The getter 'iterator' was called on null.
Receiver: null
Tried calling: iterator
════════════════════════════════════════════════════════════════════════════════════════════════════
这是基本的 stream:
final String uid;
DatabaseService({this.uid});
final CollectionReference userCollection = Firestore.instance.collection("user");
Stream<UserData> get userData{
if(userCollection.document(uid).snapshots() != null){
return userCollection.document(uid).snapshots().map(_userDataFromSnapshot);
}
else{
return null;
}
}
UserData _userDataFromSnapshot(DocumentSnapshot snapshot){
List<Map<String, dynamic>> daysMaps = List<Map<String, dynamic>>.from(snapshot.data["days"]);
List<Day> days = [];
//List<dynamic> _daysMaps = snapshot.data["days"];
if(daysMaps.length > 1){
days = daysMaps.map((day) => Day.fromMap(day)).toList();
}
else{
days.add(Day.fromMap(daysMaps[0]));
}
Map<String,dynamic> todayMap = Map<String,dynamic>.from(snapshot.data["today"]);
Day today = Day.fromMap(todayMap);
return UserData(
uid: uid,
name: snapshot.data["name"],
days: days,
today: today,
);
}
这就是我制作 StreamProvider 的地方(上面的用户 stream 是另一个):
class Wrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
if(user == null){
return Authenticate();
}
else{
return StreamProvider<UserData>.value(
value: DatabaseService(uid: user.uid).userData,
child: Home()
);
}
}
}
我不知道这里是否有错误,但这是 Home Widget:
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _currentIndex = 1;
@override
Widget build(BuildContext context) {
//getting other streams
final userdata = Provider.of<UserData>(context);
final user = Provider.of<User>(context);
print(userdata);
final AuthService _auth = AuthService();
//_auth.signOut();
List<dynamic> tabs = [
//TrainingTab
Center(child: Text("Coming Soon")),
//HomeTab
Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
DayStats(),
DayOverview(),
],
),
),
//
Center(child: FloatingActionButton(
onPressed: (){
DatabaseService(uid: user.uid).updateUserData([], Day(
burnedCalories: 300,
targetCalories: 0,
foodCalories: 0,
date: DateTime(DateTime.now().year,DateTime.now().month,DateTime.now().day,0,0,0,0,0)));
},
))
];
return userdata != null ? Scaffold(
backgroundColor: Color.fromRGBO(193, 214, 233, 1),
appBar: AppBar(
title: Text("MyApp"),
centerTitle: true,
elevation: 0.0,
actions: <Widget>[
FlatButton.icon(
onPressed: () async {
await _auth.signOut();
Navigator.pushReplacementNamed(context, "/Wrapper");
},
icon: Icon(Icons.person),
label: Text("logout")
)
],
),
body: tabs[_currentIndex],
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
currentIndex: _currentIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.fitness_center),
title: Text("Workout")
),
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: Icon(Icons.fastfood),
title: Text("Ernährung")
)
],
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
),
) : Loading();
}
}
我使用了 List.from,没有检查 null。
就我而言,我忘记检查列表中的项目是否为 null。 下面的代码帮助我从相同的错误中恢复。
if (filterRestaurantList[index].category != null) {
for (var category
in filterRestaurantList[index].category) {
if (category.categoryDetail != null) {
categoryList.add(category.categoryDetail.categoryName);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.