颤振多级下拉菜单不适用于第二次选择第一个下拉菜单

时间:2019-11-22 17:21:32

标签: flutter

我正在尝试实现依赖于多层的Dropdown。这两个Dropdown的数据都是从API请求中动态获取的。第一次工作正常。但是,当我尝试第二次更改1st Dropdown时,它显示以下错误。

我的代码如下:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:radius_admin/services/api.dart';
import 'package:radius_admin/screen/drawer_screen.dart';
import 'package:radius_admin/model/menu_item.dart';
import 'package:radius_admin/model/manager.dart';
import 'package:radius_admin/model/pop.dart';
import 'package:radius_admin/model/customer_due_details.dart';

class BillView extends StatefulWidget {
  static String route = 'bill_view';
  @override
  _BillViewState createState() => _BillViewState();
}

class _BillViewState extends State<BillView> {
  List<MenuItem> _items;

  String appUserName = ' ';
  String appUserEmail = ' ';
  String appUserPhoto = ' ';

  Manager selectedManagerItem;
  Pop selectedPopItem;

  List<Manager> _managerList = [];
  List<Pop> _popList = [];

  Manager m = Manager(id: -1, name: '--Choose--');
  Pop p = Pop(id: -1, name: '--Choose--');

  List<CustomerDueDetails> _dueList = [];

  @override
  void initState() {
    sharedPref();
    getMenu();
    getManagerList();
    super.initState();
  }

  @override
  void dispose() {
    getMenu();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Bill View'),
      ),
      drawer: SideMenu(
        context: context,
        appUserEmail: appUserEmail,
        appUserName: appUserName,
        appUserPhoto: appUserPhoto,
        items: _items,
      ),
      body: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            androidDropdownManager(),
            androidDropdownPop(),
            /*FutureBuilder(
              builder: (context, projectSnap) {
                if (projectSnap.connectionState == ConnectionState.none &&
                    projectSnap.hasData == null) {
                  //print('project snapshot data is: ${projectSnap.data}');
                  return Container();
                }
                return ListView.builder(
                  itemCount: projectSnap.data.length,
                  itemBuilder: (context, index) {
                    CustomerDueDetails data = projectSnap.data[index];
                    print(data.customerName);
                    return Column(
                      children: <Widget>[
                        //Text(data.customerName),
                      ],
                    );
                  },
                );
              },
              future: customerDueSearch(),
            ),*/
            SizedBox(
              height: 10.0,
            ),
            Container(
              padding: EdgeInsets.only(top: 30.0),
              child: Text(
                'Developed By: Md Emran Hossain & Anwar Hossain',
                style: TextStyle(
                  fontSize: 15.0,
                  color: Colors.grey,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void sharedPref() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      setState(() {
        appUserEmail = prefs.getString('user');
        appUserName = prefs.getString('userName');
        appUserPhoto = prefs.getString('userPhoto');
      });
    } catch (e) {
      print(e);
    }
  }

  getMenu() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String token = prefs.getString('token');
      var data =
          await Api().callBasicAPI(apiUrl: 'api/get-menu-list', token: token);
      List collection = data;
      setState(() {
        _items = collection.map((json) => MenuItem.fromJson(json)).toList();
      });
    } catch (e) {
      print(e);
    }
  }

  getManagerList() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String token = prefs.getString('token');
      var data = await Api()
          .callBasicAPI(apiUrl: 'api/get-manager-list', token: token);

      List collection = data;
      setState(() {
        _managerList =
            collection.map((json) => Manager.fromJson(json)).toList();
      });
    } catch (e) {
      print(e);
    }
  }

  DropdownButton<Manager> androidDropdownManager() {
    try {
      List<DropdownMenuItem<Manager>> dropdownItems = [];

      for (Manager item in _managerList ?? m) {
        var newItem = DropdownMenuItem(
          child: Text(item.name),
          value: item,
        );
        dropdownItems.add(newItem);
      }

      return DropdownButton<Manager>(
        value: selectedManagerItem,
        isExpanded: true,
        items: dropdownItems,
        onChanged: (value) {
          setState(() {
            selectedManagerItem = value;
          });
          getPopList();
        },
      );
    } catch (e) {
      print(e);
    }
  }

  getPopList() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String token = prefs.getString('token');
      var data = await Api().getPopList(
          apiUrl: 'api/get-pop-list',
          token: token,
          managerId: selectedManagerItem.id.toString());

      List collection = data;
      setState(() {
        _popList = collection.map((json) => Pop.fromJson(json)).toList();
      });
    } catch (e) {
      print(e);
    }
  }

  DropdownButton<Pop> androidDropdownPop() {
    try {
      List<DropdownMenuItem<Pop>> dropdownItems = [];
      for (Pop item in _popList) {
        var newItem = DropdownMenuItem(
          child: Text(item.name),
          value: item,
        );
        dropdownItems.add(newItem);
      }

      return DropdownButton<Pop>(
        value: selectedPopItem,
        isExpanded: true,
        items: dropdownItems,
        onChanged: (value) {
          setState(() {
            selectedPopItem = value;
          });
//          customerDueSearch();
        },
      );
    } catch (e) {
      print(e);
    }
  }

  Future customerDueSearch() async {
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String token = prefs.getString('token');
      var data = await Api().getDueBillList(
          apiUrl: 'api/get-due-bill-list',
          token: token,
          popId: selectedPopItem.id.toString());
      List collection = data;

      return _dueList =
          collection.map((json) => CustomerDueDetails.fromJson(json)).toList();
    } catch (e) {
      print(e);
    }
  }
}

错误显示以下消息:

Syncing files to device Android SDK built for x86...
I/flutter (25800): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

════════小部件库捕获到异常══════════════════════════════════ ═════════════════════

The following assertion was thrown building BillView(dirty, state: _BillViewState#aff4f):
Column's children must not contain any null values, but a null value was found at index 0

User-created ancestor of the error-causing widget was: 
  MaterialApp file:///D:/Android_App/radius_admin/lib/main.dart:15:5
When the exception was thrown, this was the stack: 
#0      new MultiChildRenderObjectWidget.<anonymous closure> (package:flutter/src/widgets/framework.dart:1691:11)
#1      new MultiChildRenderObjectWidget (package:flutter/src/widgets/framework.dart:1697:8)
#2      new Flex (package:flutter/src/widgets/basic.dart:3734:8)
#3      new Column (package:flutter/src/widgets/basic.dart:4270:8)
#4      _BillViewState.build (package:radius_admin/screen/bill_view.dart:62:16)
...

═══════════════════════════════════════════════ ══════════════════════════════════════════════════ ═══

Reloaded 2 of 680 libraries in 472ms.
I/flutter (25800): setState() called after dispose(): _BillViewState#aff4f(lifecycle state: defunct, not mounted)
    I/flutter (25800): This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.
    I/flutter (25800): The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
    I/flutter (25800): This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().
    I/flutter (25800): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 620 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem<T> item) => item.value == value).length == 1': is not true.

0 个答案:

没有答案