我在streambuilder [Flutter]中的应用中在空错误上调用了方法[[]]

时间:2020-08-29 15:26:27

标签: flutter

编辑:-实际上,我通过将!snapshot.data.documents.isEmpty更改为snapshot.data.data()解决了该问题!= null 我发现,当我尝试使用新文档将项目迁移到FlutterFire时,某些情况没有显示错误,但它们也无法正常工作。但是阅读文档后,我能够解决所有问题。

════════小部件库捕获到异常══════════════════════════════════ ═ 构建StreamBuilder时引发了以下NoSuchMethodError(脏,状态:_StreamBuilderBaseState #8fcb4): 方法'[]'在null上被调用。 接收者:null 尝试致电:

它在调试控制台中显示了该文件的错误,它曾经可以正常工作,但我没有进行任何更改,但在streambuilder中显示了nosuchmethoderror

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:skype_clone/models/call.dart';
import 'package:skype_clone/provider/user_provider.dart';
import 'package:skype_clone/resources/call_methods.dart';
import 'package:skype_clone/screens/callscreens/pickup/pickup_screen.dart';

class PickupLayout extends StatelessWidget {
  final Widget scaffold;
  final CallMethods callMethods = CallMethods();

  PickupLayout({
    @required this.scaffold,
  });

  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of<UserProvider>(context);

    return (userProvider != null && userProvider.getUser != null)
        ? StreamBuilder<DocumentSnapshot>(
            stream: callMethods.callStream(uid: userProvider.getUser.uid),
            builder: (context, snapshot) {
              if (snapshot.hasData && snapshot.data.data != null) {
                Call call = Call.fromMap(snapshot.data.data());

                if (!call.hasDialled) {
                  return PickupScreen(call: call);
                }
              }
              return scaffold;
            },
          )
        : Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
  }
}

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:skype_clone/models/call.dart';
import 'package:skype_clone/provider/user_provider.dart';
import 'package:skype_clone/resources/call_methods.dart';
import 'package:skype_clone/screens/callscreens/pickup/pickup_screen.dart';

class PickupLayout extends StatelessWidget {
  final Widget scaffold;
  final CallMethods callMethods = CallMethods();

  PickupLayout({
    @required this.scaffold,
  });

  @override
  Widget build(BuildContext context) {
    final UserProvider userProvider = Provider.of<UserProvider>(context);

    return (userProvider != null && userProvider.getUser != null)
        ? StreamBuilder<DocumentSnapshot>(
            stream: callMethods.callStream(uid: userProvider.getUser.uid),
            builder: (context, snapshot) {
              if (snapshot.hasData && !snapshot.data.documents.isEmpty) {
                Call call = Call.fromMap(snapshot.data.data());

                if (!call.hasDialled) {
                  return PickupScreen(call: call);
                }
              }
              return scaffold;
            },
          )
        : Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
  }
}

答案 1 :(得分:0)

实际上,我通过将!snapshot.data.documents.isEmpty更改为snapshot.data.data()解决了该问题!= null 我发现,当我尝试使用新文档将项目迁移到FlutterFire时,某些情况没有显示错误,但它们也无法正常工作。但是阅读文档后,我能够解决所有问题。