如何更改flutter on Change的下拉餐点上的价值

发布于 2025-01-24 13:28:30 字数 1830 浏览 1 评论 0原文

我是扑动的初学者,我只是在学习扑动,并且我陷入了这个代码中如何解决这个问题,请帮助我?

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函数(字符串)'无法分配给参数类型'void函数(字符串?)?”因为“字符串?”是无效的,“字符串”不是。

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?

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?)?' because 'String?' is nullable and 'String' isn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2025-01-31 13:28:30

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

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),
            ),
          ],
        ),
      ),
    );
  }
}
神经大条 2025-01-31 13:28:30

您需要遵循无效的安全规则,因为您的版本支持无效安全性。

只需更改您的代码;

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

我建议检查并了解什么是无效的安全性。

You need to follow null safety rules, because your version supports null safety.

Simply change your code;

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

And I suggest check and learn what null safety is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文