2
votes

I am new to flutter and trying to build a simple drop down.

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
  Widget build(BuildContext context) {
    return MaterialApp(

      title: 'My App',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: SimpleScreen()
    );
  }

}

class SimpleScreen extends StatefulWidget {
  @override
  _SimpleScreenState createState() => _SimpleScreenState();
}

class _SimpleScreenState extends State<SimpleScreen> {
  String currentValue = 'Item#1';

  List<DropdownMenuItem> _menuItems =  <DropdownMenuItem>[
    DropdownMenuItem(child: new Container(
    child: new Text ("Item#1"),
    width: 200.0, 
  )
    )
    ,
    DropdownMenuItem(child: new Container(
      child: new Text ("Item#2"),
      width: 200.0, //200.0 to 100.0
    )
    )
  ];

  @override
  Widget build(BuildContext context) {

    return new Scaffold(body:
      DropdownButton(
      value: currentValue,
      items: _menuItems,
      onChanged: onChanged,
      style: Theme.of(context).textTheme.title,
    )
    );
  }

  void onChanged(value) {
    print(value);
  }

}

When I run this code , I am getting error

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════ The following assertion was thrown building SimpleScreen(dirty, dependencies: [_LocalizationsScope-[GlobalKey#dab9d], _InheritedTheme], state: _SimpleScreenState#90ea9): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == value).length == 1': is not true.

1
What is the issue? - Liem Vo
I am trying to add code, please allow some time, thanks - user12021836

1 Answers

0
votes

You are missing the value in each menuItem.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'My App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: SimpleScreen());
  }
}

class SimpleScreen extends StatefulWidget {
  @override
  _SimpleScreenState createState() => _SimpleScreenState();
}

class _SimpleScreenState extends State<SimpleScreen> {
  String currentValue = 'Item#1';

  List<DropdownMenuItem> _menuItems = <DropdownMenuItem>[
    DropdownMenuItem(
        child: new Container(
          child: new Text("Item#1"),
          width: 200.0,
        ),
        value: "Item#1"),
    DropdownMenuItem(
        child: new Container(
          child: new Text("Item#2"),
          width: 200.0, //200.0 to 100.0
        ),
        value: "Item#2")
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: DropdownButton(
      value: currentValue,
      items: _menuItems,
      onChanged: onChanged,
      style: Theme.of(context).textTheme.title,
    ));
  }

  void onChanged(value) {
    print(value);
  }
}