Home > Blockchain >  How to put item from Dart Flutter Secure Storage stringList into listTile?
How to put item from Dart Flutter Secure Storage stringList into listTile?

Time:03-05

I have a listTile. I want to place the items from the stringList that I pulled from Secure Storage to the listTile. How can I do it?

    Future<void> listUpload() async {
    final prefences = await SharedPreferences.getInstance();
    final getTests = prefences.getStringList("tests"); // get item

    Expanded(
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return Padding(
            padding: const EdgeInsets.all(2.0),
            child: Container(
              child: ListTile(
                // title: item
              ),
            ),
          );
        },
      ),
    );
}

CodePudding user response:

you just have to wrap your text list elements in a Text widget:

    Future<void> listUpload() async {
    final prefences = await SharedPreferences.getInstance();
    final getTests = prefences.getStringList("tests"); // get item

    Expanded(
      child: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          return Padding(
            padding: const EdgeInsets.all(2.0),
            child: Container(
              child: ListTile(
                title: Text(getTests[index])
              ),
            ),
          );
        },
      ),
    );
}

CodePudding user response:

Here is the function for loading data from the Secure Storage:

Future<List<String?>> listUpload() async {
  final prefences = await SharedPreferences.getInstance();
  final getTests = prefences.getStringList("tests"); // get item
  return Future.value(getTests);
}

You must use FutureBuilder like that:

// some page of your app
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Expanded(
            child: FutureBuilder(
                future: listUpload(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  late List<String?> items;
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    // here is what should be shown if the
                    // data from the SharedPreferences has not yet been loaded
                    items = [];
                  } else if (snapshot.connectionState == ConnectionState.done &&
                      snapshot.hasData) {
                    // here is a case when items
                    // from SharedPreferences has been loaded
                    items = snapshot.data as List<String?>;
                  } else {
                    // other cases:
                    items = [];
                  }
                  // Now we can build ListView
                  return ListView.builder(
                    itemCount: items.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Padding(
                        padding: const EdgeInsets.all(2.0),
                        child: Container(
                          child: ListTile(
                            title: Text(
                              items[index].toString()
                            )),
                        ),
                      );
                    },
                  );
                })),
      ),
    );
  }
}
  • Related