【问题标题】:String stored in Shared Preferences is not loading at first in Flutter app存储在共享首选项中的字符串最初未在 Flutter 应用程序中加载
【发布时间】:2022-11-24 18:54:47
【问题描述】:

我有一个项目管理应用程序,在按卡片上的“更多”图标按钮后会显示项目的详细信息。要显示的详细信息包括从数据库中获取的项目名称和截止日期,然后使用共享首选项将其存储在本地。 'setString' 和 'getString 都运行良好,但在加载项目详细信息页面时,详细信息最初不会出现。它们仅在该页面处于活动状态时热重新加载应用程序后显示。以下是项目详细信息应用程序的代码:

import 'package:flutter/material.dart';
import 'package:mne/Actual%20Tasks/activity_widget.dart';
import 'package:mne/UserTasks/task_widget.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ProjectTask extends StatefulWidget {
  const ProjectTask({Key key}) : super(key: key);

  @override
  State<ProjectTask> createState() => _ProjectTaskState();
}

class _ProjectTaskState extends State<ProjectTask> {
  String pname;
  String pdesc;
  String pdue;

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

  Future<Null> _fetchData() async {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    pname = localStorage.getString('project_name');
    pdesc = localStorage.getString('project_desc');
    pdue = localStorage.getString('project_due');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          iconTheme: const IconThemeData(color: Colors.black),
          backgroundColor: Colors.white,
          automaticallyImplyLeading: true,
          centerTitle: true,
          title: const Text('Project Details',
              style: TextStyle(color: Colors.black))),
      body: SingleChildScrollView(
        child: Column(children: [
          Container(height: 10, color: Colors.transparent),
          // for image
          Container(
            width: 330,
            child: Image.asset('assets/images/projectbanner.png'),
          ),
          //for project name
          Container(
              padding: const EdgeInsets.only(bottom: 25, top: 15),
              child: Row(children: [
                Container(
                  padding: const EdgeInsets.only(left: 20, right: 145),
                  child: Text(pname ?? '',
                      style: const TextStyle(
                          color: Colors.black,
                          fontWeight: FontWeight.bold,
                          fontSize: 16)),
                ),
                Container(
                    padding: const EdgeInsets.only(right: 10, top: 8),
                    child: const Icon(Icons.calendar_month_outlined)),
                RichText(
                    text: TextSpan(children: [
                  const TextSpan(
                      text: 'Due: ',
                      style: TextStyle(
                          fontSize: 12,
                          fontWeight: FontWeight.bold,
                          color: Colors.black)),
                  TextSpan(
                      text: pdue ?? '',
                      style: const TextStyle(fontSize: 12, color: Colors.black))
                ])),
              ])),
          // for description title
          Container(
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.only(left: 20, bottom: 20),
              child: const Text('Description',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))),
          // for actual desc
          Container(
              padding: const EdgeInsets.only(left: 20),
              alignment: Alignment.centerLeft,
              child: Text(
                pdesc ?? '',
                style: const TextStyle(color: Colors.grey),
              )),
          // for task title
          Container(
              padding: const EdgeInsets.only(left: 20, top: 20, bottom: 20),
              alignment: Alignment.centerLeft,
              child: const Text('Tasks',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16))),
          // for task widget
          Container(height: 630, child: const ActivityWidget()),
        ]),
      ),
    );
  }
}

此图像显示了它首次加载时的样子:

这是热重载后的样子:

我怎样才能让它立即显示信息?任何帮助表示赞赏。

【问题讨论】:

    标签: flutter flutter-sharedpreference


    【解决方案1】:

    将您的 fetchData 函数更改为:

      Future<Null> _fetchData() async {
        WidgetsFlutterBinding.ensureInitialized();
        SharedPreferences localStorage = await SharedPreferences.getInstance();
        setState(() {
          pname = localStorage.getString('project_name');
          pdesc = localStorage.getString('project_desc');
          pdue = localStorage.getString('project_due');
        })
      }
    

    【讨论】:

      【解决方案2】:

      在你的 main.dart 文件中这样做:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-20
        • 2015-06-11
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-09
        相关资源
        最近更新 更多