4

火库设置我正在运行我的应用程序并收到错误消息:

“NoSuchMethodError:在 null 上调用了方法 '[]'。接收方:null。尝试调用:。”

这也发生在我的 firestore 数据库中的所有三个字段“photourl”和“total questions”上。

这个错误是在我实现提供者之后发生的,所以我不确定这是否是这个结果。

我的代码如下:

void main() {
  runApp(
      ChangeNotifierProvider(
      builder: (context) => UserModel(),
      child: MyApp(),
      ),
  );
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Profile Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Profile'),
    );
  }
}

class User {
  final int name;
  final DocumentReference reference;

  User.fromMap(Map<String, dynamic> map, {this.reference})
      : name = map['name'];

  User.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);
}

class Photo {
  final int photourl;
  final DocumentReference reference;

  Photo.fromMap(Map<String, dynamic> map, {this.reference})
      : photourl = map['photourl'];

  Photo.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);
}

class Questions {
  final int totalquestions;
  final DocumentReference reference;

  Questions.fromMap(Map<String, dynamic> map, {this.reference})
      : totalquestions = map['totalquestions'];

  Questions.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final _width = MediaQuery.of(context).size.width;
    final _height = MediaQuery.of(context).size.height;
    return Consumer<UserModel>(builder: (context, userModel, child) {
      return StreamBuilder<DocumentSnapshot>(
        stream: Firestore.instance.collection(userModel.uid)
            .document(userModel.uid).snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            return Stack(
              children: <Widget>[
                new Container(
                  color: Colors.blue,
                ),
                new Image.network(
                  snapshot.data['photourl'].toString(),
                  fit: BoxFit.fill,
                ),
                new BackdropFilter(
                    filter: new ui.ImageFilter.blur(
                      sigmaX: 6.0,
                      sigmaY: 6.0,
                    ),
                    child: new Container(
                      decoration: BoxDecoration(
                        color: Colors.blue.withOpacity(0.9),
                        borderRadius: BorderRadius.all(Radius.circular(50.0)),
                      ),
                    )),
                new Scaffold(
                  appBar: new AppBar(
                    title: new Text(widget.title),
                    centerTitle: false,
                    elevation: 0.0,
                    backgroundColor: Colors.transparent,
                  ),
                  drawer: new Drawer(
                    child: new Container(),
                  ),
                  backgroundColor: Colors.transparent,
                  body: new Center(
                    child: new Column(
                      children: <Widget>[
                        new SizedBox(
                          height: _height / 12,
                        ),
                        new CircleAvatar(
                          radius: _width < _height ? _width / 4 : _height / 4,
                          backgroundImage: NetworkImage(snapshot.data['photourl']),
                        ),
                        new SizedBox(
                          height: _height / 25.0,
                        ),
                        new Text(
                          snapshot.data['name'],
                          style: new TextStyle(fontWeight: FontWeight.bold, fontSize: _width / 15, color: Colors.white),
                        ),
                        new Padding(
                          padding: new EdgeInsets.only(top: _height / 30, left: _width / 8, right: _width / 8),
                        ),
                        new Divider(
                          height: _height / 15,
                          color: Colors.white,
                        ),
                        new Row(
                          children: <Widget>[
                            rowCell(snapshot.data['totalquestions'], 'Answers'),
                            rowCell('£ ${int.parse(snapshot.data['totalquestions']) * 2}', 'Earned'),
                          ],
                        ),
                        new Divider(height: _height / 15, color: Colors.white),
                      ],
                    ),
                  ),
                ),
              ],
            );
          } else {
            return CircularProgressIndicator();
          }
        },
      );
    });
  }
4

3 回答 3

4

该错误只是说明在[]不包含 Map 对象且相当空的变量上调用了运算符。我建议您??在访问 map 的属性之前每次都检查 null 或为 map using 提供默认值。

于 2019-09-16T13:33:31.730 回答
1

这只是猜测,但我认为您在指定流时犯了一个错误。您查询的集合是userModel.uid. 你的收藏是这样命名的吗?

于 2019-09-16T12:21:42.370 回答
1

当您使用 NetworkImage 时,您应该始终检查图像是否为空,因为它是一个异步任务。代码可能如下所示:

backgroundImage: snapshot.data['photourl'] != null ?NetworkImage(snapshot.data['photourl']) : Container(), 
于 2019-12-19T07:23:45.423 回答