Flutter StreamProvider返回空对象,即使流正在返回对象

时间:2020-01-03 11:54:19

标签: flutter dart flutter-provider

cloud firestore streams会扑朔迷离,我无法正常运行StreamProvider,我已经测试了流本身,并且成功侦听了流。

但是当我尝试访问某些子小部件中的数据时,StreamProvider返回一个null对象,这有什么用?

main.dart

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Lambda',
      theme: ThemeData(
        primarySwatch: Colors.amber,
      ),
      home: MyHomePage(),
      routes: {
        '/loads': (context) => LoadsScreen(),
        '/cust_home': (context) => CustHome()
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

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

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    final Function eq = ListEquality().equals;
    return ChangeNotifierProvider<UserFactory>(
      create: (ctx) => UserFactory.instance(),
      child: Consumer(
        builder: (ctx, UserFactory userFactory, _) {
          switch (userFactory.status) {
            case Status.Uninitialized:
              return RoleSelect();
            case Status.Unauthenticated:
            case Status.Authenticating:
              return RoleSelect();
            case Status.Authenticated:
              return MultiProvider(
                providers: [
                  StreamProvider<Driver>(
                  create: (_) =>FirestoreService(uid: userFactory.user.uid).getDriverProfile(),
                  updateShouldNotify: (d1,d2) => !eq(d1.toMap().values.toList(), d2.toMap().values.toList() )  ,),
                ],
                child: LoadsScreen(),
              );
          }
        },
      ),
    );
  }
}

在孩子中,我知道它为null,因为developer.log(driverProfile.email)抛出错误 说driverProfile对象为空

树上的孩子

@override
  Widget build(BuildContext context) {
    final Driver driverProfile = Provider.of<Driver>(context);
    developer.log(driverProfile.email);
    return Container(
          height: this.widget.height,
          width: this.widget.width,
          child: Column(
            children: <Widget>[
              Container(
                child: ListTile(
                  leading: ClipOval(
                      child: SkeletonAnimation(
                      child: Container(
                        width: this.widget.height*0.07,
                        height: this.widget.height*0.07,
                        decoration: BoxDecoration(
                          color: Colors.grey[300],
                          shape: BoxShape.circle),
                      ),
                    ),
                  ),
                  title: SkeletonAnimation(
                    child: Container(
                      width: this.widget.width*0.3,
                      height: this.widget.height*0.07,
                      decoration: BoxDecoration( borderRadius: BorderRadius.circular(20)),
                    ),
                  ),
                  subtitle: Container(
                      width: this.widget.width*0.22,
                      height: this.widget.height*0.05,
                      decoration: BoxDecoration( borderRadius: BorderRadius.circular(20)),
                    ),
                ),
              ),
              Container(),
              Container(
                child : Column(
                  children: <Widget>[
                    ListTile(
                      leading: Container(
                        width: this.widget.height*0.05,
                        height: this.widget.height*0.05,
                        child: 
                          SvgPicture.asset(
                            'assets/svgs/usericon.svg',
                            color: Colors.white,
                            semanticsLabel: 'usericon'),
                      ),
                      title: LambdaText('Username', Colors.white, 4, 24),
                      subtitle: driverProfile != null 
                      ? LambdaText(driverProfile.email, Colors.white, 3, 18)
                      : SkeletonAnimation(
                        child: Container(
                          width: this.widget.width *0.2,
                          height: this.widget.height*0.05,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(20)
                          ),
                        ),
                      ),
                    ),
                  ],)
              )
            ],
          ),
        );
  }

0 个答案:

没有答案