'Is there a way to call on difference constructors through a loop in flutter

I am designing a mobile dictionary for my mother language in flutter, which each word and its meaning are in their constructor, and I implement a search bar, as its shown in the code below

This is my words and their meanings class

class WordList {
  final String word;
  final String meaning;
  const WordList({required this.word, required this.meaning});
}

this is my called constructor in a variable

const dictionary = [
  WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'àbàdâ', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bàba', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'badà', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'céndzũ̀', meaning: 'Eternal, everlasting,forever'),
  WordList(word: 'bagábà', meaning: 'Eternal, everlasting,forever'),

];

As shown in above code, it will be so difficult for be call all WordList constructor for each words and its meanings, so I want if there will be away for me to create two list variable (word and meaning), or get it from a file, so that in const dictionary variable I will just loop through and call all the constructors with the word and its meaning

Here is my complete code for the application

import 'package:flutter/material.dart';
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
 @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final controller = TextEditingController();
  List<WordList> words = dictionary;
  List<String> listWords = worda;
  List<String> listMeanings = meaninga;
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
         appBar: AppBar(      
           title: Text('Nupe / English Dictionary'),
           elevation: 0.0,
         ),
         body: Container(
           child: Column(
             children: [
               Container(
                 color: Colors.blue,
                 padding: EdgeInsets.fromLTRB(10, 0, 10, 10),
                 child: TextField(
                   controller: controller,
                  scrollPadding: EdgeInsets.all(50),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 5, left:15),
                    filled: true,
                    fillColor: Colors.white,
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5),
                    ),
                    hintText: 'Enter Nupe/English Word...',
                    suffixIcon: Icon(Icons.search)
                  ),
                  onChanged: search,
                ),
               ),
               Container(
                 //child: Text('wornd and meaning'),
                 child: Expanded(
                    child: ListView.builder(
                    itemCount: words.length,                  
                    itemBuilder: (BuildContext contex, int index){
                      final word = words[index];
                      final listWord = listWords[index];
                      final listMeaning = listMeanings[index];
                      return ListTile(
                      tileColor: Colors.white,
                      onTap: null,
                      title: Text(word.word,
                        style: TextStyle(color: Colors.black,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      subtitle: Text(word.meaning,
                        style: TextStyle(color: Colors.black),
                      ),
                    );
                  }
                ),
                ),
              )
            ],
          ),
         ),
      );
  }
  void search(String value) {
    final suggestions = dictionary.where((word) {
      final wordd = word.word.toLowerCase();
      final input = value.toLowerCase();
      return wordd.contains(input);
    }).toList();
    setState(() => words = suggestions);
  }
}


Solution 1:[1]

A simple way to avoid writing out the constructor for each dictionary entry would be to use a map, e.g.

     List<WordList> words = {
    'àbàdâ': 'Eternal, everlasting,forever',
    'bàba': 'Eternal, everlasting,forever',
    'badà': 'Eternal, everlasting,forever',
  }
      .map((key, value) => MapEntry(key, WordList(word: key, meaning: value)))
      .values
      .toList();

Reading from a file will depend on the formats available to you, but if you can pick I would go with json. Links here should get you started:

https://docs.flutter.dev/cookbook/persistence/reading-writing-files#4-read-data-from-the-file https://docs.flutter.dev/development/data-and-backend/json#serializing-json-manually-using-dartconvert

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1