Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to inject value to parser #63

Open
ThinkontrolSY opened this issue Mar 25, 2021 · 4 comments
Open

How to inject value to parser #63

ThinkontrolSY opened this issue Mar 25, 2021 · 4 comments

Comments

@ThinkontrolSY
Copy link

I just want to use Provider to inject repository to dynamic widget, and I get some error

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

class MyParser extends WidgetParser {
  @override
  String get widgetName => "MyParser";

  @override
  Widget parse(Map<String, dynamic> map, BuildContext buildContext,
      ClickListener listener) {
    final repo = buildContext.read<MyRepo>();
    returen Text("Some thing");
}

and I get

I/flutter ( 3783): Error: Could not find the correct Provider<MyRepo> above this DynamicScada Widget
I/flutter ( 3783):
I/flutter ( 3783): This happens because you used a `BuildContext` that does not include the provider
I/flutter ( 3783): of your choice. There are a few common scenarios:
I/flutter ( 3783):
I/flutter ( 3783): - You added a new provider in your `main.dart` and performed a hot-reload.
I/flutter ( 3783):   To fix, perform a hot-restart.
I/flutter ( 3783):
I/flutter ( 3783): - The provider you are trying to read is in a different route.
I/flutter ( 3783):
I/flutter ( 3783):   Providers are "scoped". So if you insert of provider inside a route, then
I/flutter ( 3783):   other routes will not be able to access that provider.
I/flutter ( 3783):
I/flutter ( 3783): - You used a `BuildContext` that is an ancestor of the provider you are trying to read.
I/flutter ( 3783):
I/flutter ( 3783):   Make sure that DynamicScada is under your MultiProvider/Provider<MyRepo>.
I/flutter ( 3783):   This usually happens when you are creating a provider and trying to read it immediately.
I/flutter ( 3783):
I/flutter ( 3783):   For example, instead of:
I/flutter ( 3783):
I/flutter ( 3783):   ```
I/flutter ( 3783):   Widget build(BuildContext context) {
I/flutter ( 3783):     return Provider<Example>(
I/flutter ( 3783):       create: (_) => Example(),
I/flutter ( 3783):       // Will throw a ProviderNotFoundError, because `context` is associated
I/flutter ( 3783):       // to the widget that

And I Provider MyRepo in root level

@dengyin2000
Copy link
Owner

打印的内容不是说得很清楚了吗?

@ThinkontrolSY
Copy link
Author

是不是意味着不能通过provide往parse里面注入?

@dengyin2000
Copy link
Owner

parse不是widget,应该是不行的。

@TshineZheng
Copy link

刚接触这个库,也有同样想法,尝试做了个方案。
https://github.com/TshineZheng/dynamic_component
虽然没解决 provide 注入,但目的类似,实现参数传递,供各位参考,适合局部动态化。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants