NoSuchMethodError:在空颤动时调用了 getter 'uid'

时间:2021-03-02 04:39:52

标签: flutter dart

不断收到此错误:

<块引用>

NoSuchMethodError: getter 'uid' 在 null flutter 上被调用。

自定义抽屉代码

 import 'package:flutter/material.dart';
    import 'package:provider/provider.dart';
    import 'package:rideboard/models/user.dart';
    import 'package:rideboard/services/database.dart';
    import 'package:rideboard/ui/components/constants.dart';
    import 'package:rideboard/ui/components/custom_button.dart';
    import 'package:rideboard/services/auth.dart';
    
    final AuthService _mypagedrawerservice = AuthService();
    
    // ignore: must_be_immutable
    class MyDrawer extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final user = Provider.of<MyUser>(context);
        print(user.uid);
        return StreamBuilder<UserData>(
            stream: DatabaseService(uid: user.uid).userData,
            // ignore: missing_return
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                // ignore: unused_local_variable
                UserData userData = snapshot.data;
                return Drawer(
                    child: Container(
                  child: ListView(
                    children: [
                      Stack(
                        children: [
                          Container(
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                CircleAvatar(
                                    backgroundImage:
                                        AssetImage('assets/my_picture.jpg'),
                                    radius: 70),
                                SizedBox(height: 20),
                                Text(
                                  userData.firstname,
                                  style: TextStyle(
                                      fontSize: 25, fontWeight: FontWeight.bold),
                                ),
                              ],
                            ),
                            height: 230,
                            width: double.infinity,
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                  image: AssetImage('assets/PATTERN3.png'),
                                  fit: BoxFit.cover),
                            ),
                          ),
                        ],
                      ),
                      Column(
                        children: [
                          SizedBox(height: 20),
                          ListTile(
                            title: new Row(children: [
                              SizedBox(
                                width: 45,
                              ),
                              Text('Profile', style: kiconTexts),
                            ]),
                            leading: Icon(Icons.account_box, size: 23),
                            onTap: () {},
                          ),
                          SizedBox(height: 15),
                          ListTile(
                            title: new Row(children: [
                              SizedBox(
                                width: 45,
                              ),
                              Text('Filters', style: kiconTexts),
                            ]),
                            leading: Icon(Icons.drag_indicator_rounded, size: 23),
                            onTap: () {},
                          ),
                          SizedBox(height: 15),
                          ListTile(
                            title: new Row(children: [
                              SizedBox(
                                width: 12,
                              ),
                              Text('Ride/Trip History', style: kiconTexts),
                            ]),
                            leading: Icon(Icons.history, size: 23),
                            onTap: () {},
                          ),
                          SizedBox(height: 15),
                          ListTile(
                            title: new Row(children: [
                              SizedBox(
                                width: 40,
                              ),
                              Text('Payment', style: kiconTexts),
                            ]),
                            leading: Icon(Icons.payment_rounded, size: 23),
                            onTap: () {},
                          ),
                          SizedBox(height: 15),
                          ListTile(
                            title: new Row(children: [
                              SizedBox(
                                width: 40,
                              ),
                              Text('Settings', style: kiconTexts),
                            ]),
                            leading: Icon(Icons.settings, size: 23),
                            onTap: () {},
                          ),
                          SizedBox(height: 80),
                          CustomButton1(
                            colorChoice: kthemeColor.withOpacity(0.9),
                            yourConstraints:
                                BoxConstraints.expand(width: 120, height: 45),
                            widgetChoice: Text(
                              'Log Out',
                              style: kMyName,
                            ),
                            whenPressed: () {
                              _mypagedrawerservice.signOut().then((_) {
                                Navigator.of(context).pushNamedAndRemoveUntil(
                                    '/Login', ModalRoute.withName('/Rider'));
                              });
                            },
                          )
                        ],
                      ),
                    ],
                  ),
                ));
              }
            });
      }
    }

包装代码

import 'package:flutter/material.dart';
import 'package:rideboard/ui/Screens/Rider_page.dart';

import 'package:rideboard/ui/authenticate/authenticate.dart';
import 'package:rideboard/models/user.dart';
import 'package:provider/provider.dart';

    class Wrapper extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // return either the authenticate or the driver or rider screen
        var user = Provider.of<MyUser>(context);
        if (user == null) {
          return Authenticate();
        } else {
          print(user.uid);
          return RiderPage();
        }
      }
    }

Main.dart 代码

    import 'dart:async';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter_spinkit/flutter_spinkit.dart';
    import 'package:rideboard/route_generator.dart';
    import 'package:rideboard/services/appData.dart';
    import 'package:rideboard/services/auth.dart';
    import 'ui/Screens/Login_Screen.dart';
    import 'ui/components/constants.dart';
    import 'package:provider/provider.dart';
    import 'models/user.dart';
    
    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(Rideboard());
    }
    
    class Rideboard extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ChangeNotifierProvider(
          create: (context) => AppData(),
          child: StreamProvider<MyUser>.value(
            value: AuthService().user,
            child: MaterialApp(
              initialRoute: "/Wrapper",
              onGenerateRoute: RouteGenerator.generateRoute,
              debugShowCheckedModeBanner: false,
              theme: ThemeData(
                scaffoldBackgroundColor: kthemeColor.withOpacity(0.2),
                textTheme: ktextTheme,
              ),
            ),
          ),
        );
      }
    }
    
    class LoadingScreen extends StatefulWidget {
      static const String id = 'LoadingScreen';
    
      @override
      _LoadingScreenState createState() => _LoadingScreenState();
    }
    
    class _LoadingScreenState extends State<LoadingScreen> {
      //Initializes the Timer
      @override
      void initState() {
        super.initState();
    
        Timer(
          Duration(seconds: 20),
          () => Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => LoginScreen(),
            ),
          ),
        );
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Stack(
          fit: StackFit.expand,
          children: [
            Container(
              decoration: BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(
                      'assets/PATTERN3.png',
                    ),
                    fit: BoxFit.cover),
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                SizedBox(height: 20),
                Image.asset(
                  'assets/logo.png',
                  scale: 4.5,
                ),
                Text('Hitch a ride Asap!', style: kSplashScreen),
                SizedBox(height: 30),
                SpinKitWave(
                  itemBuilder: (context, index) {
                    return DecoratedBox(
                      decoration: BoxDecoration(
                        color: index.isEven ? Color(0xFF0A1768) : Colors.blue,
                      ),
                    );
                  },
                ),
                SizedBox(
                  height: 30,
                ),
                Text('By Myname', style: kMyName)
              ],
            )
          ],
        ));
      }
    }

路由设置

    import 'package:flutter/material.dart';
    import 'package:rideboard/ui/Screens/Driver_Screen1.dart';
    import 'package:rideboard/ui/Screens/Getting_Started.dart';
    import 'package:rideboard/ui/Screens/Login_Screen.dart';
    import 'package:rideboard/ui/Screens/MakeAChoiceScreen.dart';
    import 'package:rideboard/ui/Screens/Rider_page.dart';
    import 'package:rideboard/ui/Screens/Search_Screen.dart';
    import 'package:rideboard/ui/Screens/Sign_Up.dart';
    import 'ui/Screens/wrapper.dart';
    
    class RouteGenerator {
      static Route<dynamic> generateRoute(RouteSettings settings) {
        switch (settings.name) {
          case '/Wrapper':
            return MaterialPageRoute(builder: (_) => Wrapper());
          case '/Login':
            return MaterialPageRoute(builder: (_) => LoginScreen());
          case '/SignUp':
            return MaterialPageRoute(builder: (_) => SignUpPage());
          case '/GettingStarted':
            return MaterialPageRoute(builder: (_) => GettingStarted());
          case "/Rider":
            return MaterialPageRoute(builder: (_) => RiderPage());
          case "/Choice":
            return MaterialPageRoute(builder: (_) => MakeAChoice());
          case "/Drive":
            return MaterialPageRoute(builder: (_) => DriverApp());
          case "/Search":
            return MaterialPageRoute(builder: (_) => SearchScreen());
        }
      }
    }

错误:

I/flutter ( 8598): This is your address::1634, Carter Lane,
Woodbridge, Virginia I/flutter ( 8598): ══╡ EXCEPTION CAUGHT BY
WIDGETS LIBRARY
╞═══════════════════════════════════════════════════════════ I/flutter
( 8598): The following NoSuchMethodError was thrown building
MyDrawer(dirty, dependencies: I/flutter ( 8598):
[_InheritedProviderScope<MyUser>]): I/flutter ( 8598): The getter
'uid' was called on null. I/flutter ( 8598): Receiver: null I/flutter
( 8598): Tried calling: uid I/flutter ( 8598): I/flutter ( 8598): The
relevant error-causing widget was: I/flutter ( 8598):   MyDrawer
I/flutter ( 8598):  
file:///D:/App%20Development/Main%20Projects/rideboard/lib/ui/Screens/Rider_page.dart:100:16
I/flutter ( 8598): I/flutter ( 8598): When the exception was thrown,
this was the stack: I/flutter ( 8598): #0      Object.noSuchMethod
(dart:core-patch/object_patch.dart:51:5) I/flutter ( 8598): #1     
MyDrawer.build (package:rideboard/ui/components/Mydrawer.dart:16:16)
I/flutter ( 8598): #2      StatelessElement.build
(package:flutter/src/widgets/framework.dart:4749:28) I/flutter (
8598): #3      ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4675:15) I/flutter (
8598): #4      Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4654:5) I/flutter ( 8598):
#6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5) I/flutter ( 8598):
...     Normal element mounting (42 frames) I/flutter ( 8598): #48    
Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3615:14) I/flutter (
8598): #49     MultiChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6282:32) I/flutter (
8598): ...     Normal element mounting (4 frames) I/flutter ( 8598):
#53     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14) I/flutter (
8598): #54     Element.updateChild
(package:flutter/src/widgets/framework.dart:3377:20) I/flutter (
8598): #55     SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:6171:14) I/flutter (
8598): #56     Element.updateChild
(package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #57     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #58     StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #59     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#60     StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#61     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #62     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #63     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#64     StatelessElement.update (package:flutter/src/widgets/framework.dart:4756:5) I/flutter ( 8598):
#65     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #66     Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3605:39) I/flutter (
8598): #67     Element.updateChild
(package:flutter/src/widgets/framework.dart:3377:20) I/flutter (
8598): #68     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #69     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#70     ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#71     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #72     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #73     StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #74     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#75     StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#76     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #77     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #78     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#79     ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#80     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #81     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #82     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#83     ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#84     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #85     RenderObjectElement.updateChildren
(package:flutter/src/widgets/framework.dart:5693:32) I/flutter (
8598): #86     MultiChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:6293:17) I/flutter (
8598): #87     Element.updateChild
(package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #88     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #89     StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #90     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#91     StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#92     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #93     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #94     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#95     ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#96     Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #97     ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #98     StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #99     Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#100    StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#101    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #102    SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:6171:14) I/flutter (
8598): #103    Element.updateChild
(package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #104    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #105    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#106    StatelessElement.update (package:flutter/src/widgets/framework.dart:4756:5) I/flutter ( 8598):
#107    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #108    SingleChildRenderObjectElement.update
(package:flutter/src/widgets/framework.dart:6171:14) I/flutter (
8598): #109    Element.updateChild
(package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #110    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #111    StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #112    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#113    StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#114    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #115    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #116    StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #117    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#118    StatefulElement.update (package:flutter/src/widgets/framework.dart:4879:5) I/flutter ( 8598):
#119    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #120    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #121    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#122    ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#123    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #124    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #125    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#126    ProxyElement.update (package:flutter/src/widgets/framework.dart:5033:5) I/flutter ( 8598):
#127    Element.updateChild (package:flutter/src/widgets/framework.dart:3367:15) I/flutter (
8598): #128    ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4700:16) I/flutter (
8598): #129    StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4847:11) I/flutter (
8598): #130    Element.rebuild
(package:flutter/src/widgets/framework.dart:4369:5) I/flutter ( 8598):
#131    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2777:33) I/flutter (
8598): #132    WidgetsBinding.drawFrame
(package:flutter/src/widgets/binding.dart:894:21) I/flutter ( 8598):
#133    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:320:5) I/flutter ( 8598):
#134    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1117:15) I/flutter (
8598): #135    SchedulerBinding.handleDrawFrame
(package:flutter/src/scheduler/binding.dart:1055:9) I/flutter ( 8598):
#136    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:971:5) I/flutter ( 8598):
#140    _invoke (dart:ui/hooks.dart:157:10) I/flutter ( 8598): #141    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:256:5)
I/flutter ( 8598): #142    _drawFrame (dart:ui/hooks.dart:120:31)
I/flutter ( 8598): (elided 3 frames from dart:async) I/flutter (
8598): I/flutter ( 8598):

1 个答案:

答案 0 :(得分:0)

如果您使用的是 Firebase 等外部服务,则用户可以保持 null 直到完全加载。您应该检查用户的状态并显示加载指示器等,直到它不为空。 Nitesh 在您帖子的评论中也部分提到了这一点。

另见类似帖子:The getter 'displayName' was called on null. - Flutter