1
votes

so I have this simple code:

import 'package:provider/provider.dart';

class DataModel with ChangeNotifier{
  bool _isLoading = true;

  set isLoading(bool value){
    _isLoading = value;
    notifyListeners();
  }

  get isLoading => _isLoading;
}

ChangeNotifier and notifyListeners() aren't recognized.

My dependencies:

dependencies:
  flutter:
    sdk: flutter

  flutter_localizations:
    sdk: flutter

  provider: ^4.3.3

I'm very confused as to why is it happening, this is the exact setup in the installation page (and it worked in other projects).

This project is also connected to a git lab project, I don't know if it is related.

btw, it's not like that with other keywords that are in the provider package - it perfectly recognizes ChangeNotifierProvider i.e

1
where did you provide and consume your DataModel?Adnan Alshami
@AdnanAlshami currently I don't use the consume keyword, I use ChangeNotifierProvider and in it I do this: create: (context) => DataModel(),Amit Ritter

1 Answers

3
votes

You are importing an incorrect package, the correct one is package:flutter/foundation.dart or alternatively package:flutter/material.dart, try with:

import 'package:flutter/material.dart';

class DataModel with ChangeNotifier{
  bool _isLoading = true;

  set isLoading(bool value){
    _isLoading = value;
    notifyListeners();
  }

  get isLoading => _isLoading;
}

package:provider/provider.dart is used in the files where the calls to the provider are made, not where it is defined.

See the example from the docs: https://github.com/flutter/samples/blob/master/provider_shopper/lib/models/cart.dart

A hint, in VS Code if you right click on ChangeNotifier and select Go to Definition, the definition can be traced to the foundation package.