在应用程序的第一个版本中,Fflight中的提供者不会返回.在我的应用程序中,你可以 Select 应用程序的原色.例如,当我将其设置为 colored颜色 2(int表示一种 colored颜色 )时,我使用共享首选项保存它,并使用Provider在每个页面和小部件中使用它.很简单!但在该应用程序的第一个版本中,它不能获取 colored颜色 .更好地理解图像:

APP启动时:

App

当我使用导航栏时:

App

希望你能理解使用图片的问题.有没有人可以帮忙在应用程序第一次构建时立即获取 colored颜色 .

这是 colored颜色 切换的提供程序:

class ColorProvider with ChangeNotifier {
  int _selectedColor = 0;
  Color _mainColor = Colors.blue;

  ColorProvider() {
    _getColor();
  }

  int get selectedColor => _selectedColor;

  set selectedColor(int index) {
    _selectedColor = index;
    _saveColor();
    notifyListeners();
  }

  Color get mainColor => _mainColor;

  Future<void> _saveColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('_selectedColorKey', _selectedColor);
  }

  Future<void> _getColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final savedSelectedColor = prefs.getInt('_selectedColorKey');
    if (savedSelectedColor != null) {
      _selectedColor = savedSelectedColor;
    }
  }

  // This code can be used in another widget the provider and prefs only
  // saves an int so with this you can choose (this isn't too important)
  _getMainClr(int no) {
    switch (no) {
      case 0:
        _mainColor = blueClr;
        return blueClr;
      case 1:
        _mainColor = tifBlueClr;
        return tifBlueClr;
      case 2:
        _mainColor = yellowClr;
        return yellowClr;
      default:
        _mainColor = blueClr;
        return blueClr;
    }
  }
}

我试图实现这段代码,以便它可以在开始时获取getPrefs,但上面的color Provider几乎已经做到了这一点,但这段代码值得一试.

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

  Future<void> getPrefs() async {
    //Get the int selected Color
    setState(() {});
  }

推荐答案

您遇到的问题是由于从SharedPreferences获取数据的异步性质造成的.为了解决此问题,您可以在ColorProvider中创建一个名为init()的方法,并按如下方式调用它:

class ColorProvider with ChangeNotifier {
  int _selectedColor = 0;
  Color _mainColor = Colors.blue;
  bool _isInitialized = false;

  int get selectedColor => _selectedColor;

  set selectedColor(int index) {
    _selectedColor = index;
    _saveColor();
    notifyListeners();
  }

  Color get mainColor => _mainColor;

  Future<void> init() async {
    await _getColor();
    _isInitialized = true;
    notifyListeners();
  }

  Future<void> _saveColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    await prefs.setInt('_selectedColorKey', _selectedColor);
  }

  Future<void> _getColor() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    final savedSelectedColor = prefs.getInt('_selectedColorKey');
    if (savedSelectedColor != null) {
      _selectedColor = savedSelectedColor;
    }
  }

  // the rest of your code
}

像这样拨打init():

@override
void initState() {
  super.initState();
  Provider.of<ColorProvider>(context,listen:false).init();
}

快乐的编码...

Flutter相关问答推荐

Flutter -如何从布局填充(对称水平)中排除小部件?

Flutter -使用最小高度展开

如何让一个名为ONTAP的回调函数、另一个名为onTapDown的回调函数和另一个名为onTapUp的回调函数不重叠?

在fltter_widget_from_html中启动url

TextFormfield无法与自动路由一起正常工作

BlocBuilder仅生成一次,并在后续发出时停止重建

Flutter OpenID Connect无效参数:redirect_uri using Keycloak

BottomNavigationBar-我应该使用Container和Align吗?

Flutter :执行com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction时出现故障

将记录/元组用作SplayTreeMap中的键

Flutter 构建 Web 分段故障 - 无法启动事件处理程序线程

没有为类型 'Widget' 定义运算符 '[]' .try 定义运算符[]

PreferredSizeWidget类不能用作混合,因为它既不是混合类也不是混合

更改 flutter listview 中的布局

Android 模拟器在 Apple M2 芯片 EXC_BAD_ACCESS / KERN_INVALID_ADDRESS 上随机崩溃

如果不刷新页面,我看不到从 Firestore 中提取的数据

如何防止键盘将内容向上推?

Getx Flutter 在更新值时抛出空错误

在 Flutter 中,您什么时候应该更喜欢Widget继承而不是组合?

Flutter 用户过滤器