选择后出现DropdownButton错误(HTTP-JSON-FutureBuilder)

时间:2019-05-14 21:43:24

标签: dart flutter

我有一些FutureBuilder,它使用JSON响应返回一个DrobdownButton,其中填充了HTTP请求的结果。我装满了DropdownButton,一切似乎都正常。问题是选择元素后出现错误:

'package:flutter / src / material / dropdown.dart':断言失败:609行pos 15:'item == null || I / flutter(5210):item.isEmpty ||值== null || items.where(((DropdownMenuItem item)=> item.value == I / flutter(5210):value).length == 1':不正确。

我了解,由于某种原因,DropdownButton在选择后大约为空...

我尝试了以下代码:

https://inducesmile.com/google-flutter/how-to-populate-dropdownbutton-using-json-api-in-flutter/

但是在这种情况下,选择后,所选值将转到另一个小部件。我想要的是使用de value:DropdownButton的参数。

在上面的代码中,对value:参数进行了注释。我尝试了此代码,但未在该行添加任何注释,并删除了SizedBox。我唯一想要的是在同一DropdownButton上选择该值

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  Items _currentItem;

  Future<List<Items>> _getItemsData() async {
    List<Items> dataItems = new List<Items>();

    for (int i = 1; i <= 3; i++) {
      dataItems.add(
        Items.fromJson(
          {'id':i,'name':'Elem ${i}'}
        )
      );
    }

    sleep(new Duration(seconds: 5));

    return dataItems;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new FutureBuilder(
              future: _getItemsData(),
              builder: (BuildContext context, AsyncSnapshot<List<Items>> snapshot) {
                if (!snapshot.hasData) {
                  return CircularProgressIndicator();
                } else {
                  return new Container(
                    width: 280,
                    child: new DropdownButton<Items>(
                      items: snapshot.data.map((items) => DropdownMenuItem<Items>(
                        child: Text(items.name),
                        value: items,
                      )).toList(),
                      onChanged: (Items value) {
                        setState(() {
                          _currentItem = value;
                        });
                      },
                      isExpanded: true,
                      value: _currentItem,
                      hint: Text('Select one...')
                    )
                  );
                }
              }
            )
          ]
        )
      )
    );
  }
}

class Items {
  int id;
  String name;

  Items({this.id, this.name});

  factory Items.fromJson(Map<String, dynamic> json) {
    return Items (
        id: json['id'],
        name: json['name']
    );
  }
}

0 个答案:

没有答案