Flutter 未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'uid'

时间:2021-01-09 14:26:18

标签: flutter google-cloud-firestore firebase-authentication flutter-layout flutter-provider

首先,我想说我已经看过之前关于这个错误的所有帖子,但没有一个能解决我的问题,这就是我发布它的原因。

其实,我已经理解了问题,但无法解决。所以,下面的 dart 文件是我的 HomeScreen()。

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:mukti/authentication/firestore_service.dart';
import 'package:mukti/schedule/task.dart';
import 'package:mukti/schedule/taskdata.dart';
import 'package:provider/provider.dart';
import 'add_class.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;

class HomeScreen extends StatefulWidget {

  static final String routeName = 'homeScreen';

  final auth.User firebaseUser;
  HomeScreen({this.firebaseUser});

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

class _HomeScreenState extends State<HomeScreen> {
  
  final FirestoreService firestoreService = FirestoreService();

  @override
  Widget build(BuildContext context) {
    
    print("HomeScreen");
    print(widget.firebaseUser);

    return Scaffold(
      body: SafeArea(
        child: SizedBox.expand(
          child: Padding(
            padding: const EdgeInsets.all(48.0),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [

                SingleChildScrollView(
                  child: Container(
                      width: MediaQuery.of(context).size.width,
                      height: 450,
                      color: Colors.yellow[100],
                      child: generateTaskList(),
                    ),
                ),
                SizedBox(height: 45),

                /* Add Class Button */
                Center(
                    child: GestureDetector(
                      onTap: () {
                        Navigator.push(
                          context, MaterialPageRoute(
                            builder: (context) => AddClass(),
                          ),
                        );
                      },
                      child: Container(
                        height: 75,
                        width: 75,
                        decoration: BoxDecoration(
                          shape: BoxShape.circle,
                          boxShadow: [
                              BoxShadow(
                              color: Colors.grey,
                              offset: Offset(0, 4), //(x,y)
                              blurRadius: 1.0,
                            ),
                          ],
                          color: Color(0xFFF9A826),
                        ),
                        child: Icon(
                          Icons.add,
                          size: 35,
                          color: Colors.white,
                        ),
                      ),
                    ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget generateTaskList() {

    //print("Firebase User : ${widget.firebaseUser.uid}");

    Provider.of<TaskData>(context, listen: false).loadTaskList(widget.firebaseUser);
    print('List Generated');

    var taskListLength = Provider.of<TaskData>(context, listen: false).getTaskListCount();
    return Consumer<TaskData>(
      builder: (context, taskData, child) => ListView.builder(
        itemCount: taskData.taskList.length,
        itemBuilder: (context, index) {
          print("TaskList");
          return Container(
            padding: EdgeInsets.all(16.0),
            decoration: new BoxDecoration (
              borderRadius: BorderRadius.circular(10),
              color: Color(0xFFF9A826),
            ),
            child: ListTile(
              title: Text(
                taskData.taskList[index].description ?? 'default',
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w600,
                ),
              ),
              subtitle: Text(
                "${Util.getFormattedDate(taskData.taskList[index].scheduledDateTime)}" ?? 'default',
                style: TextStyle(
                  color: Colors.white54,
                ),
              ),
            ),
          );
        }
      )
    );
  }
}

class Util {
  static String getFormattedDate(DateTime dateTime) {
    return new DateFormat("EEE, MMM d, y").format(dateTime);
  }
}

最初,firebaseUser 不为空,我已经检查过它并且它正在应用程序中打印数据但是当我从 AddClass() 添加更多条目并再次返回到 HomeScreen() 时,firebaseUser 变为空并且没有数据显示不再在应用中。

下面的代码是我的 AddClass() 代码:

import 'package:flutter/material.dart';
import 'package:mukti/authentication/authService.dart';
import 'package:mukti/authentication/firestore_service.dart';
import 'package:mukti/schedule/scheduled_date.dart';
import 'package:mukti/schedule/task.dart';
import 'package:mukti/ui_pages/main_screen/timepicker.dart';
import 'package:provider/provider.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:firebase_auth/firebase_auth.dart' as auth;

import 'home_screen.dart';

class AddClass extends StatefulWidget {

  static final String routeName = 'addClass';
  @override
  _AddClassState createState() => _AddClassState();
}

class _AddClassState extends State<AddClass> {

  final FirestoreService firestoreService = FirestoreService();

  @override
  Widget build(BuildContext context) {
    print("Add Class Screen");
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Color(0xFFFFFFE5),
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,

          children: [

            /* Meeting url TextField Widget */

            /* Description TextField Widget */

            /* Calender */

            /* Add Class Button */
            Expanded(
              child: GestureDetector(
                onTap: () async{

                  auth.User firebaseUser = await Provider.of<AuthService>(context, listen: false).getUser();
                  DateTime scheduledDateTime = Provider.of<ScheduledDate>(context, listen: false).scheduledDateTime;

                  print(scheduledDateTime);
                  print(firebaseUser);

                  final task = Task(
                    link: 'xyz',
                    isDone: false,
                    description: 'xyz',
                    scheduledDateTime: scheduledDateTime,
                  );

                  firestoreService.addTask(firebaseUser, task);
                  print('Task Added');

                  Navigator.popAndPushNamed(context,
                      HomeScreen.routeName,
                      arguments: firebaseUser,
                  );
                },
                child: Align(
                  alignment: FractionalOffset.bottomCenter,
                  child: Container(
                      height: 50,
                      width: MediaQuery.of(context).size.width,
                      decoration: BoxDecoration(
                        color: Color(0xFFF9A826),
                      ),
                      child: Center(
                          child: Text(
                              'SCHEDULE CLASS',
                              style: Theme.of(context).textTheme.bodyText1.copyWith(
                                  color: Colors.white,
                                fontWeight: FontWeight.w700,
                                fontSize: 16,
                                letterSpacing: 0.5,
                              ),
                          )
                      )
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

实际上,我已经删除了不必要的代码,所以在向数据库中添加了一个类之后,我返回到 HomeScreen,但这次 firebaseUser 变为 null,尽管我在 HomeScreen 接收 null 的路由的参数中发送它。这是我的问题。

我该如何解决这个问题...?

如果有人需要更多信息,请随时问我。 谢谢

0 个答案:

没有答案