找不到正确的提供者

时间:2019-11-16 11:55:45

标签: flutter flutter-provider

我开始学习Flutter,并且尝试使用provider。 我已经完成了一些教程,现在开始我的副项目,但是我被这些错误所困扰:

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

当我用Multiproviders包装MaterialApp时,它应该在祖先中了。。。这个错误对我来说没有意义,我肯定是个新手,而且我还缺少一些明显的东西...

无论如何,这是我的代码,谢谢您的帮助。

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')
        ],
      ),
    );
  }
}

中耕机型号

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();
  }
}

1 个答案:

答案 0 :(得分:1)

这里是我的版本的工作方式,我没有做任何重大更改,我的配置是 IntelliJ Idea,Flutter 1.9.1 + hotfix.6•稳定的通道和iOS虚拟设备

如果您在运行下一个代码时遇到任何问题,请与我联系,我会尽力提供帮助。

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();
  }
}