簡體   English   中英

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

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

我是 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),),
          ],
        ),
      ),
    );
  }

}

錯誤顯示此消息:

錯誤:無法將參數類型“void Function(String)”分配給參數類型“void Function(String?)?” 因為“字符串?” 可以為 null 而 'String' 不是。

你需要遵守null安全規則,因為你的版本支持null安全。

只需更改您的代碼;

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

我建議檢查並了解 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