dengyin2000 / dynamic_widget

A Backend-Driven UI toolkit, build your dynamic UI with json, and the json format is very similar with flutter widget code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to inject value to parser

ThinkontrolSY opened this issue · comments

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

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

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

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

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