12
Future<void> _getUserData() async {
  final database = Firestore.instance;
  try {
    FirebaseUser _currentUser = await FirebaseAuth.instance.currentUser();
    String authid =_currentUser.uid;
    String email = _currentUser.email;
    Map<String, String> userData = {
      'AuthUserId':'$authid',
      'Email':'$email',
      'FullName': '',
      'MobileNum': '',
      'Address': '',
      'City/Village': '',
      'District': '',
      'State': '',
      'PinCode': '',
    };
   database.collection('UserData').add(userData).catchError((e) {
     print(e);
    });
 } catch (e) {
    print(e);
 }
}
@override
void initState() {
    super.initState();
    _getUserData();
  }

"I am trying to insert the data into the cloud firestore and i have called this function in initState() but i ham getting error as i have mentioned above

7 Answers 7

21

Make sure you are using the latest cloud_firestore version. Then execute:

flutter clean
flutter packages get

Update, Starting since cloud_firestore version 0.14.0:

setData() was replaced with set(). Other useful link:

The getter 'instance' isn't defined for the type 'Firestore'

9
  • I have the latest cloud_firestrore and I had done the same as you mentioned but after updating the flutter it is happening before it is working fine Jan 26, 2020 at 9:47
  • what flutter version are u using? Jan 26, 2020 at 9:49
  • Flutter 1.12.13+hotfix.5 • channel stable • github.com/flutter/flutter.git Framework • revision 27321ebbad (7 weeks ago) • 2019-12-10 18:15:01 -0800 Engine • revision 2994f7e1e6 Tools • Dart 2.7.0 Jan 26, 2020 at 9:51
  • 1
    Did u close the ide? May 6, 2020 at 15:01
  • 1
    @Bawantha add another question with your pubspec.yaml and explaination and add it here in the comment so I can check, also ur build.gradle May 6, 2020 at 15:02
9

for my case, I performed steps as below:

flutter clean
flutter packages get

uninstall app

update: /android/app/build.gradle

defaultConfig {
    ...
    multiDexEnabled true
}

run again.

hope this helps

3

I closed the IDE (Android Studio) and did flutter clean. After that I restarted the IDE and created a new AVD. The MissingPluginException was gone after that and I had the following error:

D8: Cannot fit requested classes in a single dex file

I fixed this adding multiDexEnabled true to the defaultConfig in /android/app/build.gradle

defaultConfig {
    ...
    multiDexEnabled true
}

I don't know if this is releated to your problem, but thats how I solved the error.

2

My problem was caused by editing the firebase instance settings after I've used the firestore in the previous page which u can't do and it didn't give me the correct error for it until I delete this folder

%AppData%\Local\Pub\Cache\hosted\pub.dartlang.org\firebase_core_platform_interface-2.0.0

and then It correctly gave me the error and so I just applied the settings in main.dart

Widget build(BuildContext context) 
{
   return FutureBuilder(
      future: Firebase.initializeApp(),
      builder: (context, snapshot) 
      {
         if (snapshot.hasError) {
            return Text('error');
         }

         if (snapshot.connectionState == ConnectionState.done) {
            FirebaseFirestore.instance.settings = Settings(
            persistenceEnabled: true,
            cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED,
            );

          return MaterialApp(....
         }
      }
   );
}
1

Stop the running app and rerun. It may be because you added the firestore dependency while the app is already running in debug mode. In this case the hot reload or restart will not work if you to call methods of newly added dependency. You have to stop the running app and rerun. (This solved my issue).

Do this before rerunning. - flutter clean - flutter packages get

0

If you are working on Visual Studio Code and you are running your project on an IOS simulator, then it might not work. You can run your project from Xcode and it will work with no issues, you have to open the file that is .xcworkspace.

0

I tried this and it helped me

flutter clean
flutter packages get

Later, Uninstall App and try reinstalling it ✌🏻

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.