简体   繁体   English

如何在 Flutter 的 onChange 中更改 DropdownButton 的值

[英]How to change value on DropdownButton in onChange in Flutter

I am a beginner in the flutter I'm just learning flutter and I am stuck in this code how to solve this please help me?我是 flutter 的初学者 我刚刚学习 flutter 并且我被困在这段代码中如何解决这个问题请帮助我?

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'My Application',
      home: book(),
    );
  }

}
class book extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {

    return _bookstate();
  }

}
class _bookstate extends State<book>{
  String namebook = "";
  var writter = ['A','B','C'];
  var _currentItemSelected = 'A';

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('Stateful Widget'),

      ),
      body: Container(
        margin: EdgeInsets.all(20.0),
        child: Column(
          children:<Widget> [
            TextField(
              onChanged: (String userInput){
                setState(() {
                  namebook=userInput;
                });
              },

            ),
            DropdownButton<String>(
              items: writter.map((String dropDownStringItem){
                return DropdownMenuItem<String>(
                  value: dropDownStringItem,
                  child: Text(dropDownStringItem),
                );
              }).toList(),
              onChanged: (String newValueSelected){
               setState(() {
                 this._currentItemSelected = newValueSelected;
               });
              },
              value: _currentItemSelected,
            ),
            Text("Enter book name id $namebook",style: TextStyle(fontSize:20.0),),
          ],
        ),
      ),
    );
  }

}

and error show this message:错误显示此消息:

Error: The argument type 'void Function(String)' can't be assigned to the parameter type 'void Function(String?)?'错误:无法将参数类型“void Function(String)”分配给参数类型“void Function(String?)?” because 'String?'因为“字符串?” is nullable and 'String' isn't.可以为 null 而 'String' 不是。

You need to follow null safety rules, because your version supports null safety.你需要遵守null安全规则,因为你的版本支持null安全。

Simply change your code;只需更改您的代码;

onChanged: (String? newValueSelected) {
    setState(() {
      this._currentItemSelected = newValueSelected!;
    });
  },

And I suggest check and learn what null safety is.我建议检查并了解 null 安全性是什么。


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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const Book(),
    );
  }
}

class Book extends StatefulWidget {
  const Book({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _Bookstate();
  }
}

class _Bookstate extends State<Book> {
  String namebook = "";
  var writter = ['A', 'B', 'C'];
  var _currentItemSelected = 'A';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Stateful Widget'),
      ),
      body: Container(
        margin: const EdgeInsets.all(20.0),
        child: Column(
          children: <Widget>[
            TextField(
              onChanged: (String userInput) {
                setState(() {
                  namebook = userInput;
                });
              },
            ),
            DropdownButton<String>(
              items: writter.map((String dropDownStringItem) {
                return DropdownMenuItem<String>(
                  value: dropDownStringItem,
                  child: Text(dropDownStringItem),
                );
              }).toList(),
              onChanged: (String? newValueSelected) {
                setState(() {
                  _currentItemSelected = newValueSelected!;
                });
              },
              value: _currentItemSelected,
            ),
            Text(
              "Enter book name id $namebook",
              style: const TextStyle(fontSize: 20.0),
            ),
          ],
        ),
      ),
    );
  }
}

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

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