简体   繁体   English

在flutter中从JSON创建动态小部件

[英]create dynamic widgets from JSON in flutter

I am trying to create dynamic widgets from JSON getting from API.我正在尝试从 API 获取的 JSON 创建动态小部件。 I just follow the blog https://medium.com/@limonadev/rendering-flutter-widgets-from-json-easily-3db02490519e But after doing all the stuff we are getting some error "/C:/Users/user/AppData/Local/Pub/Cache/hosted/pub.dartlang.org/json_dynamic_widget-1.4.1/lib/src/builders/json_scaffold_builder.dart:246:7: Error: No named parameter with the name 'resizeToAvoidBottomPadding'."我只是关注博客https://medium.com/@limonadev/rendering-flutter-widgets-from-json-easily-3db02490519e但是在做完所有的事情之后我们得到了一些错误“/C:/Users/user/AppData /Local/Pub/Cache/hosted/pub.dartlang.org/json_dynamic_widget-1.4.1/lib/src/builders/json_scaffold_builder.dart:246:7: 错误:没有名为“resizeToAvoidBottomPadding”的命名参数。” My main.dart file is:我的 main.dart 文件是:

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:json_dynamic_widget/json_dynamic_widget.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Simple Example',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: DynamicText());
  }
}

class DynamicText extends StatefulWidget {
  final url = 'https://medium-json-dynamic-widget.herokuapp.com/mainpage';

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

class _DynamicTextState extends State<DynamicText> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      builder: (context, AsyncSnapshot<http.Response> snapshot) {
        if (snapshot.hasData) {
          var widgetJson = json.decode(snapshot.data.body);
          var widget = JsonWidgetData.fromDynamic(
            widgetJson,
          );
          return widget.build(context: context);
        } else {
          return Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
      },
      future: _getWidget(),
    );
  }

  Future<http.Response> _getWidget() async {
    return http.get(widget.url);
  }
}


class SimpleText extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Static Widget'),
      ),
      body: Center(
        child: Text('This is a very important message!'),
      ),
    );
  }
}

and pubspec.yaml file is: pubspec.yaml 文件是:

 name: json_to_v
description: A new Flutter project.
publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  http: ^0.12.2
  json_dynamic_widget: ^1.0.3

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

if I upgrade json_dynamic_widget: ^1.0.3 to ^2.1.0 We get following error:如果我升级json_dynamic_widget: ^1.0.3 到 ^2.1.0我们得到以下错误:

Because json_dynamic_widget >=2.0.0 <2.1.0 depends on json_schema2 ^2.0.0 and json_schema2 >=2.0.0 <=2.0.0 depends on rest_client ^2.0.0, json_dynamic_widget >=2.0.0 <2.1.0 requires rest_client ^2.0.0 or json_schema2 ^2.0.0+1.因为 json_dynamic_widget >=2.0.0 <2.1.0 依赖于 json_schema2 ^2.0.0 和 json_schema2 >=2.0.0 <=2.0.0 依赖于 rest_client ^2.0.0,所以 json_dynamic_widget >=2.0.0 <2.1.0 需要 rest_client ^2.0.0 或 json_schema2 ^2.0.0+1。 And because json_dynamic_widget >=2.1.0 depends on json_schema2 ^2.0.0+1, json_dynamic_widget >=2.0.0 requires rest_client ^2.0.0 or json_schema2 ^2.0.0+1.并且因为 json_dynamic_widget >=2.1.0 取决于 json_schema2 ^2.0.0+1,json_dynamic_widget >=2.0.0 需要 rest_client ^2.0.0 或 json_schema2 ^2.0.0+1。 And because json_schema2 >=2.0.0+1 depends on rest_client ^2.0.1+1 which depends on http ^0.13.0, json_dynamic_widget >=2.0.0 requires http ^0.13.0.并且因为 json_schema2 >=2.0.0+1 依赖于依赖于 http ^0.13.0 的 rest_client ^2.0.1+1,json_dynamic_widget >=2.0.0 需要 http ^0.13.0。 So, because json_to_v depends on both http ^0.12.2 and json_dynamic_widget ^2.0.0, version solving failed.所以,因为 json_to_v 依赖于 http ^0.12.2 和 json_dynamic_widget ^2.0.0,版本解析失败。 pub get failed (1; So, because json_to_v depends on both http ^0.12.2 and json_dynamic_widget ^2.0.0, version solving failed.) pub get failed (1; 所以,因为 json_to_v 依赖于 http ^0.12.2 和 json_dynamic_widget ^2.0.0,版本解析失败。)

And If we comment below the line and related code then the error has gone.如果我们在该行和相关代码下方进行评论,那么错误就消失了。

import 'package:json_dynamic_widget/json_dynamic_widget.dart';

Please let me know where I am doing wrong.请让我知道我哪里做错了。 Thanks谢谢

尝试将此添加到您的 pubspec.yaml:http:^0.13.3 json_dynamic_widget:^3.0.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM