Flutter ListView中的颤振载荷未来列表?

Flutter ListView中的颤振载荷未来列表?,flutter,Flutter,您好,我如何在ListView或ListViebuilder中加载此列表 Future<List<bool>> getBoolList() async{ List<bool> prefList = []; var sharedPreferences = await SharedPreferences.getInstance(); Set<String> keys = sharedPreferences.getKeys(); fo

您好,我如何在ListView或ListViebuilder中加载此列表

 Future<List<bool>> getBoolList() async{
  List<bool> prefList = [];
  var sharedPreferences = await SharedPreferences.getInstance();
  Set<String> keys = sharedPreferences.getKeys();

  for(int i=0; i<keys.length ; i++){
    bool value = sharedPreferences.getBool(keys.elementAt(i));
    prefList.add(value);
  }
  
  return prefList;
}

List<bool> list = await getBoolList();
Future getBoolList()异步{
List prefList=[];
var sharedPreferences=等待sharedPreferences.getInstance();
Set keys=SharedReferences.getKeys();
对于(int i=0;i_favoriteState();
}
类_favoriteState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“收藏夹”),
),
正文://MyList
);
}
}
FutureBuilder(
future:getBoolList(),
建设者:(背景,未来){
if(!future.hasData)return Container();//如果列表为空,则显示空容器
否则{
列表=future.data;
返回ListView.builder(
itemCount:list.length,
itemBuilder:(上下文,索引){
return//Your widget Here;//放置您的widget,如container、decoratedBox、listTiles、button等
}
);
}
}
),
示例

    FutureBuilder<List<bool>>(
      future: getBoolList(),
      builder: (context, future){
      if(!future.hasData)return Container(); // Display empty container if the list is empty
      else {
         List<bool> list = future.data;
         return ListView.builder(
          itemCount: list.length,
          itemBuilder: (context, index){
          return Container(
           child: Text(list[index].toString())
           );
          }
         );
       }
      }
    ),
FutureBuilder(
future:getBoolList(),
建设者:(背景,未来){
if(!future.hasData)return Container();//如果列表为空,则显示空容器
否则{
列表=future.data;
返回ListView.builder(
itemCount:list.length,
itemBuilder:(上下文,索引){
返回容器(
子项:文本(列表[index].toString())
);
}
);
}
}
),
那是我的最爱

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

// ignore: must_be_immutable
class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

class _FavoritenState extends State<Favoriten> {



  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text('Favorites'),
      ),
      body: // MyList
    );
  }
}
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

Future<List<bool>> getBoolList() async {
  List<bool> prefList = [];
  var sharedPreferences = await SharedPreferences.getInstance();
  Set<String> keys = sharedPreferences.getKeys();

  for (int i = 0; i < keys.length; i++) {
    bool value = sharedPreferences.getBool(keys.elementAt(i));
    prefList.add(value);
  }

  return prefList;
}

class _FavoritenState extends State<Favoriten> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Favorites'),
      ),
      body: FutureBuilder<List<bool>>(
          future: getBoolList(),
          builder: (context, future) {
            if (!future.hasData)
              return Container(child: Text('X'),); // Display empty container if the list is empty
            else {
              List<bool> list = future.data;
              return ListView.builder(
                  itemCount: list.length,
                  itemBuilder: (context, index) {
                    return Container(child: Text(list[index].toString()));
                  });
            }
          }),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:shared_preferences/shared_preferences.dart”;
类Favoriten扩展StatefulWidget{
@凌驾
_FavoriteState createState()=>\u FavoriteState();
}
未来的getBoolList()异步{
List prefList=[];
var sharedPreferences=等待sharedPreferences.getInstance();
Set keys=SharedReferences.getKeys();
for(int i=0;i
这是细节。飞镖在这里,我保存布尔

 @override
  _DetailsState createState() => _DetailsState();
}

const String spKey = 'myBool';

class _DetailsState extends State<Details> {


SharedPreferences sharedPreferences;
bool isfavorit;

@override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((SharedPreferences sp) {
      sharedPreferences = sp;
      isfavorit = sharedPreferences.getBool('${widget.id}');
      // will be null if never previously saved
      if (isfavorit == null) {
        isfavorit = false;
        persist(isfavorit); // set an initial value
      }
      setState(() {});
    });
  }

    void persist(bool value) {
    setState(() {
      isfavorit = value;
    });
    sharedPreferences?.setBool('${widget.id}', value);
  }

// ignore: missing_return
IconData favicon() {
 if (isfavorit == true) {
    return Icons.favorite; 
 } else
  if (isfavorit == false) {
   return Icons.favorite_border;
  }
}

// ignore: missing_return
Color favicolor() {
 if (isfavorit == true) {
    return Colors.red; 
 } else
  if (isfavorit == false) {
   return Colors.white;
  }
}

void changefav() {
 if (isfavorit == true) {
    return persist(false); 
 } else
  if (isfavorit == false) {
   return persist(true);
  }
}


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: Icon(
              favicon(),
              color: favicolor(),
            ),
           onPressed: () => changefav(),
          ),
        ],
        title: Text(
          AppLocalizations.of(context).translate(widget.name),
          style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
      ),
@覆盖
_DetailsState createState()=>\u DetailsState();
}
常量字符串spKey='myBool';
类_DetailsState扩展状态{
SharedReferences SharedReferences;
布尔喜欢它;
@凌驾
void initState(){
super.initState();
SharedReferences.getInstance()。然后((SharedReferences sp){
SharedReferences=sp;
isfavorit=SharedReferences.getBool('${widget.id}');
//如果以前从未保存,则将为空
if(isfavorit==null){
isfavorit=false;
persist(isfavorit);//设置初始值
}
setState((){});
});
}
void persist(布尔值){
设置状态(){
isfavorit=值;
});
SharedReferences?.setBool(“${widget.id}”,值);
}
//忽略:缺少返回
Iconda favicon(){
if(isfavorit==true){
返回图标。收藏;
}否则
if(isfavorit==false){
返回Icons.favorite_边框;
}
}
//忽略:缺少返回
彩色favicolor(){
if(isfavorit==true){
返回颜色。红色;
}否则
if(isfavorit==false){
返回颜色。白色;
}
}
void changefav(){
if(isfavorit==true){
返回persist(false);
}否则
if(isfavorit==false){
返回persist(true);
}
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
行动:[
图标按钮(
图标:图标(
favicon(),
颜色:favicolor(),
),
按下时:()=>changefav(),
),
],
标题:正文(
AppLocalizations.of(context.translate)(widget.name),
样式:TextStyle(颜色:Colors.white,fontwweight:fontwweight.bold),
),
标题:对,
),
完整示例

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

class _FavoritenState extends State<Favoriten> {
 
  Future<List<bool>> getBoolList() async {
    List<bool> prefList = [];
    var sharedPreferences = await SharedPreferences.getInstance();
    Set<String> keys = sharedPreferences.getKeys();

    for (int i = 0; i < keys.length; i++) {
      bool value = sharedPreferences.getBool(keys.elementAt(i));
      prefList.add(value);
    }
    print('list: $prefList');
    return prefList;
  }

  SharedPreferences sharedPreferences;
  bool isfavorit;

  @override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((SharedPreferences sp) {
      sharedPreferences = sp;
      isfavorit = sharedPreferences.getBool('boolname');
      // will be null if never previously saved
      if (isfavorit == null) {
        isfavorit = false;
        persist(isfavorit); // set an initial value
      }
      setState(() {});
    });
  }

  void persist(bool value) {
    setState(() {
      isfavorit = value;
    });
    sharedPreferences?.setBool('boolname', value);
  }

  // ignore: missing_return
  Color favicolor() {
    if (isfavorit == true) {
      return Colors.red;
    } else if (isfavorit == false) {
      return Colors.white;
    }
  }

  void changefav() {
    if (isfavorit == true) {
      return persist(false);
    } else if (isfavorit == false) {
      return persist(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Favorites'),
      ),
      body: Column(
        children: [
          FutureBuilder<List<bool>>(
              future: getBoolList(),
              builder: (context, future) {
                if (!future.hasData)
                  return Container(
                    child: Text('No Favorites :('),
                  ); // Display empty container if the list is empty
                else {
                  List<bool> list = future.data;
                  return ListView.builder(
                      itemCount: list.length,
                      itemBuilder: (context, index) {
                        return Container(child: Text(list[index].toString()));
                      });
                }
              }),
          RaisedButton(child: Text('Red is Saveed, White not Saved') ,onPressed: changefav),
          Container(
            width: double.infinity,
            height: 50,
            color: favicolor(),
          )
        ],
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“package:shared_preferences/shared_preferences.dart”;
类Favoriten扩展StatefulWidget{
@凌驾
_FavoriteState createState()=>\u FavoriteState();
}
类_favoriteState扩展了状态{
未来的getBoolList()异步{
List prefList=[];
var sharedPreferences=等待sharedPreferences.getInstance();
Set keys=SharedReferences.getKeys();
for(int i=0;iimport 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Favoriten extends StatefulWidget {
  @override
  _FavoritenState createState() => _FavoritenState();
}

class _FavoritenState extends State<Favoriten> {
 
  Future<List<bool>> getBoolList() async {
    List<bool> prefList = [];
    var sharedPreferences = await SharedPreferences.getInstance();
    Set<String> keys = sharedPreferences.getKeys();

    for (int i = 0; i < keys.length; i++) {
      bool value = sharedPreferences.getBool(keys.elementAt(i));
      prefList.add(value);
    }
    print('list: $prefList');
    return prefList;
  }

  SharedPreferences sharedPreferences;
  bool isfavorit;

  @override
  void initState() {
    super.initState();

    SharedPreferences.getInstance().then((SharedPreferences sp) {
      sharedPreferences = sp;
      isfavorit = sharedPreferences.getBool('boolname');
      // will be null if never previously saved
      if (isfavorit == null) {
        isfavorit = false;
        persist(isfavorit); // set an initial value
      }
      setState(() {});
    });
  }

  void persist(bool value) {
    setState(() {
      isfavorit = value;
    });
    sharedPreferences?.setBool('boolname', value);
  }

  // ignore: missing_return
  Color favicolor() {
    if (isfavorit == true) {
      return Colors.red;
    } else if (isfavorit == false) {
      return Colors.white;
    }
  }

  void changefav() {
    if (isfavorit == true) {
      return persist(false);
    } else if (isfavorit == false) {
      return persist(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Favorites'),
      ),
      body: Column(
        children: [
          FutureBuilder<List<bool>>(
              future: getBoolList(),
              builder: (context, future) {
                if (!future.hasData)
                  return Container(
                    child: Text('No Favorites :('),
                  ); // Display empty container if the list is empty
                else {
                  List<bool> list = future.data;
                  return ListView.builder(
                      itemCount: list.length,
                      itemBuilder: (context, index) {
                        return Container(child: Text(list[index].toString()));
                      });
                }
              }),
          RaisedButton(child: Text('Red is Saveed, White not Saved') ,onPressed: changefav),
          Container(
            width: double.infinity,
            height: 50,
            color: favicolor(),
          )
        ],
      ),
    );
  }
}