【问题标题】:FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#7140d) The getter 'isEmpty' was called on nullFutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#7140d) 在 null 上调用了 getter 'isEmpty'
【发布时间】:2021-07-03 13:34:02
【问题描述】:

出现以下错误: 请帮帮我。

The following NoSuchMethodError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#7140d):
The getter 'isEmpty' was called on null.
Receiver: null
Tried calling: isEmpty

这是我的代码:

constants.dart

final usersRef = _fireStore.collection('users');


profile_screen.dart
FutureBuilder<DocumentSnapshot>(
      future: usersRef.doc(widget.visitedUserId).get()
      builder: (BuildContext context,AsyncSnapshot snapshot){
        if(!snapshot.hasData){
          print("Snapshot : ${usersRef.doc(widget.visitedUserId).get()}");
          print("data : ${snapshot.data.data()}");
          return Center(
            child: CircularProgressIndicator(
              valueColor: AlwaysStoppedAnimation(kTweeterColor),
            ),
          );
        }else{
          UserModel userModel = UserModel.fromDoc(snapshot.data);
          print("usermodel : ${userModel}");
          return ListView(
            physics: const BouncingScrollPhysics(parent: AlwaysScrollableScrollPhysics()),
            children: [
              Container(
                height: 150,
                decoration: BoxDecoration(
                  color: kTweeterColor,
                  image: userModel.coverImage.isEmpty
                      ? null
                      : DecorationImage(fit: BoxFit.cover,
                      image: NetworkImage(userModel.coverImage)),
                ),

user_model.dart

import 'package:cloud_firestore/cloud_firestore.dart';

class UserModel {
  String id;
  String name;
  String profilePicture;
  String email;
  String bio;
  String coverImage;

  UserModel(
      {this.id,
      this.name,
      this.profilePicture,
      this.email,
      this.bio,
      this.coverImage});

   factory UserModel.fromDoc(DocumentSnapshot doc){
    return UserModel(
      id:doc.id,
      name: doc.data()['name'],
      email: doc.data()['email'],
      profilePicture:doc.data()['profilePicture'],
      bio: doc.data()['bio'],
      coverImage: doc.data()['coverImage'],
    );
  }
}

其他问题 ***** 代码前

  factory UserModel.fromDoc(DocumentSnapshot doc){
    return UserModel(
       String id;
       String name;
       String profilePicture;
       String email;
       String bio;
       String coverImage; 

      id:doc.id,
      name: doc.data()['name'],
      email: doc.data()['email'],
      profilePicture:doc.data()['profilePicture'],
      bio: doc.data()['bio'],
      coverImage: doc.data()['coverImage'],
        

之后******************

       String id;
       String name;
       String profilePicture="";
       String email;
       String bio;
       String coverImage =""; 


 factory UserModel.fromDoc(DocumentSnapshot doc){
return UserModel(
  id:doc.id,
  name: doc['name'],
  email: doc['email'],
  profilePicture:doc['profilePicture'],
  bio: doc['bio'],
  coverImage: doc['coverImage'],
);

}

    ***********Current Error****************

      ======== Exception caught by widgets library 
=======================================================
Bad state: field does not exist within the DocumentSnapshotPlatform
The relevant error-causing widget was: 
   FutureBuilder<DocumentSnapshot> 

file:///C:/src/flutterwork/flutter_twitter/lib/Screens/ProfileScreen.dart:64:15

    I/chatty  ( 1966): uid=10009(com.example.flutter_twitter) FirestoreWorker 
    identical 2 lines  
    W/flutter_twitte( 1966): Accessing hidden method Lsun/misc/Unsafe;- 
  >getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
   W/flutter_twitte( 1966): Accessing hidden method 
 Ldalvik/system/CloseGuard;- 
  >close()V (greylist,core-platform-api, linking, allowed)

【问题讨论】:

  • 尝试将initialData 设置为您的FutureBuilder

标签: android flutter dart google-cloud-firestore flutter-layout


【解决方案1】:

如果变量为空,isEmpty 返回错误。

喜欢这个

    String a;
    String b="";
    print(a.isEmpty); //error, because null
    print(b.isEmpty); //true, because empty string

需要完成的更新。

// image: userModel.coverImage.isEmpty
       image: userModel.coverImage==null?
                      ? null
                      : DecorationImage(fit: BoxFit.cover,
                      image: NetworkImage(userModel.coverImage)),
                ),

【讨论】:

  • 谢谢,但我已经在声明了。尚未解决装饰:盒子装饰()颜色:kTweeter颜色,图像:userModel.coverImage。是空的。 ?空值。 : 装饰图片(fit: BoxFit.cover, image: 网络图片(userModel.coverImage), ),
  • userModel.coverImage==null?执行后错误消失非常感谢。
  • @申民鐡解决了你的问题,请采纳。
猜你喜欢
  • 2020-11-16
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 2020-06-28
  • 1970-01-01
  • 2021-04-27
  • 2020-10-24
  • 2022-11-14
相关资源
最近更新 更多