Не удалось найти правильного поставщика <X>над этим виджетом Y во флаттере с навигационными потоками - PullRequest
1 голос
/ 22 октября 2019

Я не понимаю, почему провайдер здесь не принимается.

Мой main.dart выглядит следующим образом.

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: ChangeNotifierProvider<ContentDS>(
        builder: (_) => ContentDS(),
        child: Content(),
      )
    );
  }
}

Провайдер прекрасно работает в Content Class. Теперь я перехожу от Контента к Контенту, используя этот код

Navigator.of(ctx).push(MaterialPageRoute(
                builder: (context) =>
                    ContentDetails(viewedList: listElement),
              ))

В Контенте, я просто пытаюсь снова получить доступ к Провайдеру

 final ds = Provider.of<ContentDS>(context);

Но возникает следующая ошибка

Ошибка: не удалось найти правильного провайдера над этим виджетом ContentDetails

Чтобы исправить, пожалуйста:

  • Убедитесь, что провайдер является предком этого виджета ContentDetails* Предоставить типы для поставщика * Предоставить типы для потребителя * Предоставить типы для Provider.of () * Всегда использовать импорт пакетов. Пример: import 'package:my_app/my_code.dart'; * Ensure the correct context` используется.

Content-Details code

class ContentDetails extends StatefulWidget {
  ToDoList viewedList;

  ContentDetails({this.viewedList});

  @override
  _ContentDetailsState createState() => _ContentDetailsState();
}

class _ContentDetailsState extends State<ContentDetails> with SingleTickerProviderStateMixin{

  bool isOpened = false;
  AnimationController _animationController;
  Animation<Color> _buttonColor;
  Animation<double> _animateIcon;
  Animation<double> _translateButton;
  Curve _curve = Curves.easeOut;
  double _fabHeight = 56.0;

  initState() {
    _animationController =
    AnimationController(vsync: this, duration: Duration(milliseconds: 500))
      ..addListener(() {
        setState(() {});
      });
    _animateIcon =
        Tween<double>(begin: 0.0, end: 1.0).animate(_animationController);
    _buttonColor = ColorTween(
      begin: Colors.blue,
      end: Colors.red,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Interval(
        0.00,
        1.00,
        curve: Curves.linear,
      ),
    ));
    _translateButton = Tween<double>(
      begin: _fabHeight,
      end: -14.0,
    ).animate(CurvedAnimation(
      parent: _animationController,
      curve: Interval(
        0.0,
        0.75,
        curve: _curve,
      ),
    ));
    super.initState();
  }

  @override
  dispose() {
    _animationController.dispose();
    super.dispose();
  }

  animate() {
    if (!isOpened) {
      _animationController.forward();
    } else {
      _animationController.reverse();
    }
    isOpened = !isOpened;
  }

  @override
  Widget build(BuildContext context) {
    final ds = Provider.of<ContentDS>(context);

    print(" ds.count ---> " + ds.count.toString());
    if (widget.viewedList != null){
      debugPrint(" widget.viewedList.primaryID ---> " + widget.viewedList.primaryID);
    }else{
      debugPrint(" It is null");
    }
    return
     Hero(
        tag: (widget.viewedList != null) ? widget.viewedList.primaryID : 'Hero',
        child: Scaffold(
          appBar: new AppBar(
            backgroundColor: ListStatusHelper.getBgGenericColor(widget.viewedList?.status ?? ListStatus.NONE),
            title: Text('${getListTitle()}'),
            elevation: 0.0,
          ),
          floatingActionButton:  Wrap(
            direction: Axis.vertical,
            children: <Widget>[
              transformWorks(edit(), 3),
              transformWorks(save(), 2),
              transformWorks(inbox(),1),
              toggleButton()
            ],
          ),
          body: Container(
              color: ListStatusHelper.getBgGenericColor(widget.viewedList?.status ?? ListStatus.NONE),
              child: const Text("Once Provider is available, need to work on this section");

            ),
          ),
    );
  }

  Widget transformWorks(Widget btn, int pos){
    return Transform(
      transform: Matrix4.translationValues(
        0.0,
        _translateButton.value * pos,
        0.0,
      ),
      child: btn,
    );
  }

  Widget inbox() {
    return Container(
      child: FloatingActionButton(
        heroTag: null,
        onPressed: null,
        tooltip: 'Inbox',
        elevation: 0.0,
        child: Icon(Icons.inbox),
      ),
    );
  }

  Widget edit() {
    return Container(
      child: FloatingActionButton(
        heroTag: null,
        onPressed: null,
        tooltip: 'Edit',
        elevation: 0.0,
        child: Icon(Icons.edit),
      ),
    );
  }

  Widget save() {
    return Container(
      child: FloatingActionButton(
        heroTag: null,
        onPressed: null,
        tooltip: 'Save',
        elevation: 0.0,
        child: Icon(Icons.save),
      ),
    );
  }

  Widget toggleButton(){
    return  FloatingActionButton(
      heroTag: null,
      backgroundColor: _buttonColor.value,
      onPressed: animate,
      tooltip: 'Toggle',

      child: AnimatedIcon(
        icon: AnimatedIcons.menu_close,
        progress: _animateIcon,
      ),
    );
  }

  String getListTitle() {
    if (widget.viewedList != null) {
      return widget.viewedList.listTitle;
    } else {
      return 'New List';
    }
  }
}

Я разработчик iOS, который пытается изучить флаттер, и яподумал, что поставщик должен быть доступен для всех классов, принадлежащих к навигационному потоку, без использования какого-либо DI.

Помогите мне понять, что мне не хватает. Как следует использовать провайдера в случае навигационных потоков, таких как вышеупомянутые случаи.

Заранее спасибо

1 Ответ

0 голосов
/ 24 октября 2019

Ход ChangeNotifier выше MyApp:

void main() {
  runApp(ChangeNotifierProvider(
      builder: (context) => ContentDS(),
      child: MyApp()));
}
...