Zainab Rangwala
Zainab Rangwala

Reputation: 1

Undefined name 'Provider' in flutter

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?

Upvotes: 0

Views: 1382

Answers (3)

Sayan Banerjee
Sayan Banerjee

Reputation: 31

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';

Upvotes: 0

Moaz El-sawaf
Moaz El-sawaf

Reputation: 3089

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';

Upvotes: 1

Tanmoy Karmakar
Tanmoy Karmakar

Reputation: 262

You need to import the provider package on the current page

import 'package:provider/provider.dart';

Upvotes: 1

Related Questions