Home > OS >  How to change value on DropdownButton in onChange in Flutter
How to change value on DropdownButton in onChange in Flutter

Time:04-28

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.

CodePudding user response:

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.

CodePudding user response:


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