Flutter 颤振中的下拉按钮问题

Flutter 颤振中的下拉按钮问题,flutter,Flutter,我在使用Flatter(1.0)中的Dropdownbutton小部件时遇到问题,它经常抛出错误 以下是我的代码: import 'package:flutter/material.dart'; void main() => runApp(new Myapp()); class Myapp extends StatefulWidget { @override State<StatefulWidget> createState() { return new M

我在使用Flatter(1.0)中的Dropdownbutton小部件时遇到问题,它经常抛出错误

以下是我的代码:

import 'package:flutter/material.dart';

void main() => runApp(new Myapp());

class Myapp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new Myappstate();
  }
}

class Myappstate extends State<Myapp> {
  String value = '';
  List<String> values = [];
  void init() {
    values.addAll(['one', 'two']);
    value = values.elementAt(0);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("drop down demo"),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new DropdownButton(
              items: values.map((String val) {
                return new DropdownMenuItem(
                  value: val,
                  child: Text(val),
                );
              }).toList(),
              onChanged: null,
            )
          ],
        ),
      ),
    );
  }
}
我试图添加这个非常基本的小部件,但由于某些原因,我一直无法找到问题


请告知我能找到的任何帮助。

您必须包装在MaterialApp中,并在initState中加载初始数据

检查此代码,但有细微更改:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomeapp(),
    );
  }
}

class MyHomeapp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyHomeappstate();
  }
}

class MyHomeappstate extends State<MyHomeapp> {
  String value = '';
  List<String> values = [];
  @override
  void initState() {
    super.initState();
    setState(() {
      values.addAll(['one', 'two']);
      value = values.elementAt(0);
    });
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("drop down demo"),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new DropdownButton(
              hint: Container(
                child: Text('Select option'),
              ),
              items: values.map((String val) {
                return new DropdownMenuItem(
                  value: val,
                  child: Text(val),
                );
              }).toList(),
              onChanged: (val) {
                debugPrint('selected option: $val');
              },
            )
          ],
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:MyHomeapp(),
);
}
}
类MyHomeapp扩展StatefulWidget{
@凌驾
状态createState(){
返回新的MyHomeappstate();
}
}
类MyHomeappstate扩展了状态{
字符串值=“”;
列表值=[];
@凌驾
void initState(){
super.initState();
设置状态(){
addAll(['one','two']);
value=values.elementAt(0);
});
}
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
标题:新文本(“下拉演示”),
),
主体:新容器(
子:新列(
儿童:[
新下拉按钮(
提示:容器(
子项:文本(“选择选项”),
),
项:values.map((字符串val){
返回新的DropdownMenuItem(
值:val,
子项:文本(val),
);
}).toList(),
一旦更改:(val){
debugPrint('selected option:$val');
},
)
],
),
),
);
}
}
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomeapp(),
    );
  }
}

class MyHomeapp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyHomeappstate();
  }
}

class MyHomeappstate extends State<MyHomeapp> {
  String value = '';
  List<String> values = [];
  @override
  void initState() {
    super.initState();
    setState(() {
      values.addAll(['one', 'two']);
      value = values.elementAt(0);
    });
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("drop down demo"),
      ),
      body: new Container(
        child: new Column(
          children: <Widget>[
            new DropdownButton(
              hint: Container(
                child: Text('Select option'),
              ),
              items: values.map((String val) {
                return new DropdownMenuItem(
                  value: val,
                  child: Text(val),
                );
              }).toList(),
              onChanged: (val) {
                debugPrint('selected option: $val');
              },
            )
          ],
        ),
      ),
    );
  }
}