Issue
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
),
),
);
},
),
);
}
Solution
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()
)),
),
);
},
);
})),
),
);
}
}
Answered By - Sergey Lobanov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.