How can I populate dropdown using sqflite?

122

Please change child: Text(projectModel.title) to child: Text(project.title)

           items: newList.map((project) => DropdownMenuItem<Project>(
             child: Text(project.title),
             value: project,
           )
           ).toList()
Share:
122
Jorden
Author by

Jorden

Updated on December 16, 2022

Comments

  • Jorden
    Jorden over 1 year

    I can not populate DropDown from database.

        Future<List<Map<String, dynamic>>> getProjectsMapList() async {
        Database db = await this.database;
    
        var result = await db.query(projectTable);
        return result;
        }
    
    Future<List<Project>> getProjectsList() async {
    
    var projectsMapList = await getProjectsMapList(); // Get 'Map List' from database
    int count = projectsMapList.length;         // Count the number of map entries in db table
    
    List<Project> projectList = List<Project>();
    // For loop to create a 'categories List' from a 'Map List'
    for (int i = 0; i < count; i++) {
      projectList.add(Project.fromMapObject(projectsMapList[i]));
    }
    return projectList;
    }
    

    Project Class where I created DropDown but no values are display in dropdown.

    Project projectModel;
    List<Project> newList = <Project> [];
    @override
      void initState() {
        super.initState();
        _getProjectList();
    
      }
      void _getProjectList() async {
        final List<Project> _listPj = await databaseHelper.getProjectsList();
        setState(() {
          newList = _listPj;
        });
      } 
    

    DropDown

                   DropdownButtonFormField<Project>(
                   hint: Text('Project'),
                   value: projectModel,
                   onChanged: (Project value){
                     setState(() {
                       projectModel = value;
                     });
                   },
    
                   items: newList.map((project) => DropdownMenuItem<Project>(
                     child: Text(projectModel.title),
                     value: project,
                   )
                   ).toList()
               ),
    

    Model Class

    
      int _id;
      String _title;
      String _date;
    
    
    
      Project(this._title, this._date);
    
      int get id => _id;
    
      String get title => _title;
    
      String get date => _date;
    
    
      set id(int value) {
        this._id = value;
      }
    
      set title(String newTitle) {
        if (newTitle.length <= 255) {
          this._title = newTitle;
        }
      }
    
    
      set date(String newDate) {
        this._date = newDate;
      }
    
      // Convert a Note object into a Map object
      Map<String, dynamic> toMap() {
    
        var map = Map<String, dynamic>();
        if (id != null) {
          map['id'] = _id;
        }
        map['title'] = _title;
    
        map['date'] = _date;
    
    
        return map;
      }
    
      // Extract a Note object from a Map object
      Project.fromMapObject(Map<String, dynamic> map) {
        this._id = map['id'];
        this._title = map['title'];
        this._date = map['date'];
    
      }
    }
    

    This is the code but I can not understand why data is not displaying in dropdown can anyone help me please ? I am getting NoSuchMethodError :The getter 'title' was called on NULL but I dont know where I did miatake ?

    • Jorden
      Jorden over 4 years
      NoSuchMethodError :The getter 'title' was called on NULL..
    • Jorden
      Jorden over 4 years
      Sir actually after changes error is displaying.