Home > Blockchain >  type 'Null' is not a subtype of type 'Map<String, dynamic>' - Flutter
type 'Null' is not a subtype of type 'Map<String, dynamic>' - Flutter

Time:02-16

I'm getting the following error while fetching data from firestore but I know this error is not about how I'm fetching data. It is related to the null safety which was added in latest flutter update. I'm not so much familier with it.

════════ Exception caught by provider ══════════════════════════════════════════
The following assertion was thrown:
An exception was throw by _MapStream<DocumentSnapshot, CustData> listened by

StreamProvider<CustData>, but no `catchError` was provided.

Exception:
type 'Null' is not a subtype of type 'Map<String, dynamic>'

════════════════════════════════════════════════════════════════════════════════
class MainDataProvider extends StatefulWidget {
  const MainDataProvider({Key? key}) : super(key: key);

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

class _MainDataProviderState extends State<MainDataProvider> {
 @override
  Widget build(BuildContext context) {
    User? user = FirebaseAuth.instance.currentUser; 

    StreamProvider custDataProvider = StreamProvider<CustData>.value(
      initialData: CustData.initial(),
      value: DatabaseService(uid: user?.uid).getCustData, 
       // ^^^^^^^^ I'm getting error while getting custdata
    );

    return MultiProvider(
      providers: [custDataProvider],
      child: const Scaffold(
        body: HomeView(),
      ),
    );
  }
}
  Stream<CustData> get getCustData =>
      custCollection.doc(uid).snapshots().map(_custDataFromSnapshot);

  CustData _custDataFromSnapshot(DocumentSnapshot snapshot) => CustData.fromMap(snapshot.data());

To avoid this error "The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'" I added "?" after below parameter but after putting "?" I'm getting the above error. I know I'm missing a small piece of puzzle

class CustData {
  String custID;
  String name;
  String phoneNo;
  String email;
  Map<String, dynamic> favs;
  ReferDetails referDetails;

  CustData({ ... });

// To avoid this error "The argument type 'Map<String, dynamic>?' can't be assigned to the parameter type 'Map<String, dynamic>'" I added "?" after below parameter but after putting "?" I'm getting above error
//  B E L O W    H E R E 
  factory CustData.fromMap(Map<String, dynamic>? map) {
    return CustData(
      custID: map!['custID'] ?? '',
      name: map['name'] ?? '',
      phoneNo: map['phoneNo'] ?? '',
      email: map['email'] ?? '',
      favs: map['favs'] ?? {},
      referDetails: ReferDetails.fromMap(map['referDetails']),
    );
  }
}

Here is database view

enter image description here

CodePudding user response:

You're telling the compiler that map might be null by putting the ? on the type, but after that you're stating that map will never be null by putting the ! on map in the custId line. Probably removing the ! will suffice.

CodePudding user response:

When you using Stream Provider and Calling a using firebase then you have to wait for first data which came from firebase....

Currently you call user?.uid without waiting it...So that's why its throw Error...

Check the condition for null avoiding...then use this user variable....

  • Related