Cedric Royer-Bertrand
Cedric Royer-Bertrand

Reputation: 741

Could not find correct provider

I'm starting to learn Flutter and I'm trying to use provider. I've follow a few tutorials and now starting my side project but I'm stuck with these error:

Error: Could not find the correct Provider<CultivatorModel> above this CultivatorComponent Widget

As I wrapped the MaterialApp with the Multiproviders, it should be in the ancestor... That error does not make sense to me, I'm certainly to new to flutter and I'm missing something obvious...

Anyway, here is my code, thanks for your help.

main.dart

import 'package:provider/provider.dart';

import 'package:flutter_app/components/cultivator_component.dart';
import 'package:flutter_app/models/incarnation_model.dart';
import 'package:flutter_app/models/cultivator_model.dart';

void main() {
  runApp(
      MultiProvider(
          providers: [
            ChangeNotifierProvider(builder: (context) => IncarnationModel()),
            ChangeNotifierProvider(builder: (context) => CultivatorModel()),
          ],
          child: GameApp())
  );
}

class GameApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CultivatorComponent(),
    );
  }
}

cultivator_component.dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:flutter_app/Models/cultivator_model.dart';

class CultivatorComponent extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    final cultivatorModel = Provider.of<CultivatorModel>(context);
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('${cultivatorModel.cultivator.name} is here')
        ],
      ),
    );
  }
}

cultivator_model

import 'package:flutter/foundation.dart';

import 'package:flutter_app/domain/cultivator.dart';
import 'package:flutter_app/domain/incarnation.dart';

class CultivatorModel with ChangeNotifier{
  Cultivator _cultivator;

  Cultivator get cultivator => _cultivator;

  CultivatorModel(){
    _cultivator = Cultivator("MyCultivator");
  }

  bool canCreateIncarnation() => _cultivator.canCreateIncarnation();

  void addIncarnation(Incarnation incarnation){
    _cultivator.incarnations.add(incarnation);
    notifyListeners();
  }
}

Upvotes: 1

Views: 541

Answers (1)

dubace
dubace

Reputation: 1621

Here is how my version works, I didn't do any major changes and my configuration is IntelliJ Idea, Flutter 1.9.1+hotfix.6 • channel stable and iOS virtual device

If you would have any problem with running the next code, please contact me and I try to help.

import 'dart:core';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

void main() {
  runApp(
    MultiProvider(
      providers: [
        ChangeNotifierProvider(builder: (context) => AnotherModel()),
        ChangeNotifierProvider(builder: (context) => CultivatorModel()),
      ],
      child: GameApp())
  );
}

class GameApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: CultivatorComponent(),
    );
  }
}

class CultivatorComponent extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    final cultivatorModel = Provider.of<CultivatorModel>(context);
    final anotherModel = Provider.of<AnotherModel>(context);
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('${cultivatorModel.cultivator} is here'),
          Text('${anotherModel.modelNum} is here')
        ],
      ),
    );
  }
}

class CultivatorModel with ChangeNotifier{
  String _cultivator;

  String get cultivator => _cultivator;

  CultivatorModel(){
    _cultivator = "MyCultivator";
  }

  void addIncarnation(String incarnation){
    _cultivator = incarnation;
    notifyListeners();
  }
}



class AnotherModel with ChangeNotifier{
  int _modelNum;

  int get modelNum => _modelNum;

  AnotherModel(){
    _modelNum = 55;
  }

  void changeNumber(int num){
    _modelNum = num;
    notifyListeners();
  }
}

Upvotes: 1

Related Questions