Home > Mobile >  Undefined name 'Provider' in flutter
Undefined name 'Provider' in flutter

Time:06-18

I am getting an error called "Undefined name 'Provider' in flutter. Here is the code snippet for it:

addData() async {
UserProvider _userProvider = Provider.of(context, listen : false);
await _userProvider.refreshUser();}

Can someone help me figure this out?

CodePudding user response:

You need to import the provider package on the current page

import 'package:provider/provider.dart';

CodePudding user response:

Provider is a powerful State Management package which has to be added to the project and imported in order to use it.

If you haven't added the package, then add it by the following command in the project folder in Terminal/Command Prompt:

flutter pub add provider

Then in the file containing the snippet, import the package at the top like the following:

import 'package:provider/provider.dart';

CodePudding user response:

Check if you have following dependency added in your pubspec.yaml file

dependencies:
  provider: ^6.0.3

If not add it or simply run in your terminal

flutter pub add provider

After getting your dependency import the provider package in your page:

import 'package:provider/provider.dart';
  • Related