Home > OS >  The function can't be unconditionally invoked because it can be 'null'. Try adding a
The function can't be unconditionally invoked because it can be 'null'. Try adding a

Time:06-30

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:intl/intl.dart';
import 'add_task.dart';
import 'description.dart';

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String uid = '';
  @override
  void initState() {
    getuid();
    super.initState();
  }

  getuid() async {
    FirebaseAuth auth = FirebaseAuth.instance;
    final User user = await auth.currentUser();
    setState(() {
      uid = user.uid;
    });
  }

The function auth.currentUser() can't be unconditionally invoked because it can be 'null'. Try adding a null check ('!'). I added a null check but it still does not work. May I please know how to fix this error?

CodePudding user response:

There's an issue in your getuid() function. Correct function will be like.

getuid() {
    final FirebaseAuth auth = FirebaseAuth.instance;
    final User? user = auth.currentUser;
    if (user != null) {
      setState(() {
        uid = user.uid;
      });
    }
  }
  • Related