NoSuchMethodError:方法'[]'在null上被调用。接收者:null。尝试调用:[](“照片网址”)

时间:2019-09-16 14:56:30

标签: flutter google-cloud-firestore consumer flutter-provider

我最近在我的应用程序中实现了提供者和使用者,现在出现错误:“ NoSuchMethodError:方法'[]'在null上调用。接收器:null。尝试在运行我的应用程序时调用:“。对于上下文,这是我的firestore数据库的屏幕截图。firestore database

现在,我有一个main.dart和user_model.dart文件(均在下面)。我尝试将字符串更改为包括'users'和'testuser',但两者都返回null错误。

有人可以让我知道我要去哪里或者对实施有误解吗?

P.S。当我删除提供者和使用者时,该应用程序将运行(但显然我需要使用提供者和使用者来提取所需的数据)。

main.dart:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:ui' as ui;
import 'package:provider/provider.dart';
import 'package:flutter_appprofilescreenv1/user_model.dart';

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();
            }
          });
    }
    );
    }


  Widget rowCell(String count, String type) => new Expanded(
      child: new Column(
        children: <Widget>[
          new Text(
            '$count',
            style: new TextStyle(color: Colors.white),
          ),
          new Text(type,
              style: new TextStyle(
                  color: Colors.white, fontWeight: FontWeight.normal))
        ],
      ));
}

user_model.dart:

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';

class UserModel extends ChangeNotifier {

  String uid = 'users';
}

0 个答案:

没有答案