Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

google cloud firestore - Flutter Unhandled Exception: NoSuchMethodError: The getter 'uid' was called on null

First of all, I would like to say I have seen all the previous posts on this error but none of them resolved my issue and that's why I am posting it.

Actually, I have understood the problem but unable to resolve it. So, the below dart file is my 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);
  }
}

Initially, firebaseUser is not null, I have crossed checked it and it is printing the data in the app but when I add more entries from AddClass() and returns to HomeScreen() again, firebaseUser becomes null and no data is shown in the app anymore.

The below code is my AddClass() code:

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,
                              ),
                          )
                      )
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Actually, I have removed unnecessary codes, so after adding one more class to the database, I return to HomeScreen but this time firebaseUser becomes null, although I am sending it in the argument of the routes HomeScreen is receiving null. This is my problem.

How can I resolve this..?

If anyone needs more information, feel free to ask me. Thanks

question from:https://stackoverflow.com/questions/65643761/flutter-unhandled-exception-nosuchmethoderror-the-getter-uid-was-called-on-n

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...