0

I'm trying to use provider with template, but Provider.of seems not working like that.

Logs:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ The following ProviderNotFoundError was thrown building MyChildWidget(dirty): Error: Could not find the correct Provider above this MyChildWidget Widget

To fix, please:

  • Ensure the Provider is an ancestor to this MyChildWidget Widget
  • Provide types to Provider
  • Provide types to Consumer
  • Provide types to Provider.of()
  • Always use package imports. Ex: `import 'package:my_app/my_code.dart';
  • Ensure the correct context is being used.

Do you know any solution ?

Sample to show the problem:

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Provider<MyChildState>.value(
          value: MyChildState("Hello world !"),
          child: MaterialApp(
            home: MyChildWidget(),
          )),
    );
  }
}

abstract class MyParentWidget<State extends MyParentState> extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final State state = Provider.of(context);
    return Text(state.text);
  }
}

abstract class MyParentState {
  final String text;

  MyParentState(this.text);
}

class MyChildWidget<MyChildState> extends MyParentWidget{
}

class MyChildState extends MyParentState {
  MyChildState(String text) : super(text);
}
Lyofen
  • 631
  • 6
  • 15

1 Answers1

0

At a first glance over your code, I can see that you are passing MyChildState("Hello world !") as the value, but later in MyParentWidget class you are requesting a State object final State state = Provider.of(context);.

Try requesting MyChildState instead of State.

My first impression after reading your code is that you are coming from a different programming language and now you are trying Flutter. A friendly advice would be keep everything as simple as possible.

All the abstraction and how the code is organized might confuse you later when you review the code you wrote instead of helping you.

Andrei
  • 330
  • 1
  • 3
  • 12
  • Yes i'm coming from Android. Here i would like to share UI logic with 'common widget' to inherit. I use bloc to provide stream for each widget, but if 2 widgets have some common UI logic i would like to extract this logic inside parent widget (so parent will use parent state to apply common logic, and child can override this behavior for his changes). – Lyofen Nov 29 '19 at 10:30
  • But like you said, i can try to extract state provide from parent, each child recover his state with provider.of, and create methods in parent widget to call from childs. – Lyofen Nov 29 '19 at 10:32
  • I suggest you go out there and try a few tutorials and read about Flutter state management. Your perspective of Flutter apps is slightly confusing. In my projects, I never used abstract classes for UI purposes, like having a parent and a child, and then the parent has some functionality that the child can override. Usually, every Widget represents an element from the screen. You want to model it as a Widget that has children? Add a children argument in the constructor, like the `Column` widget does. I recommend you go through the existing widgets' implementation to understand Flutter better – Andrei Nov 29 '19 at 10:54
  • For example for 2 screens, i need to enter email, with showing error if regex wrong etc... So i'll create 'EmailWidget' and use it in 2 screens. But if a third screen need to enter email but errors are showed with different text, i wouldn't duplicate all the widget, just rewrite method showing errors. – Lyofen Nov 29 '19 at 11:04
  • In this case i need common widget, but 'common state' is certainly the wrong way. For others case i need same state but some widgets with same behaviors need additionals infos, that's why i thought about override state without rewrite all the widget – Lyofen Nov 29 '19 at 11:07