简体   繁体   English

Flutter Globals失去了价值

[英]Flutter Globals losing their value

Okay, I am sure this is probably an easy to answer question, but after much searching I can't put my finger on the solution. 好的,我敢肯定这可能是一个容易回答的问题,但是经过大量搜索之后,我无法将手指放在解决方案上。

How does one in Flutter/Dart create a variable in app XYZ that maintains its' value during the time a user moves away from the app, uses another app, allows the phone to go to sleep, etc., then opens the XYZ app again. Flutter / Dart中的一个如何在应用程序XYZ中创建一个变量,该变量在用户离开该应用程序,使用另一个应用程序,让手机进入睡眠状态等期间保持其值,然后再次打开XYZ应用程序。

I've created a tiny little app that demonstrates the issue as shown below. 我创建了一个小小的应用程序,演示了此问题,如下所示。

The static variable Globals.UID can be incremented upwards, but if the app is moved away from (what term describes using another app correctly?) then as noted the phone goes to sleep, when the user comes back to the app the Globals.UID variable is reset back to -1. 静态变量Globals.UID可以向上递增,但是如果将应用程序移开(用正确的术语描述了另一个应用程序?),则如前所述,手机进入睡眠状态,当用户返回到该应用程序时,Globals.UID变量重置为-1。

Sometimes the reset is not immediate and I have to let the IOS phone sleep for perhaps 5 minutes to recreate the behavior. 有时重置不是立即进行的,我必须让IOS手机休眠5分钟以重新创建此行为。

Basically I need Globals.UID to retain its' value until the app is actually exited. 基本上,我需要Globals.UID保留其值,直到应用程序实际退出为止。

Any insight would truly be appreciated. 任何见解将不胜感激。

import 'package:flutter/material.dart';

import 'package:test_flutter/Globals.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;

      Globals.UID ++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            Text ( "Global variable is:")
            , Text ( Globals.UID.toString() )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}

And in a different file called Globals.dart 并在另一个名为Globals.dart的文件中

class Globals {

  static int    UID = -1;
}

Thank you! 谢谢!

Actually this happens to when you don't import with reference. 实际上,这发生在您不参考导入的情况下。 This is a known issues which is already fixed, may be not landed yet. 这是一个已解决的已知问题,可能尚未解决。 You can check the issue here . 您可以在此处检查问题。

Please try import package_name/globals.dart instead of just import globals.dart if you are doing so. 请尝试import package_name/globals.dart而不是仅import globals.dart

Hope that helps! 希望有帮助!

Ultimately, the solution appears to be solved mostly by using the shared_preferences library. 最终,该解决方案似乎主要是通过使用shared_preferences库来解决的。 That solution brings with it a good many documented problems, but to maintain data while the app is placed in the background, or even killed by perhaps the user, shared_preferences appears to be the way to go. 该解决方案带来了很多已记录在案的问题,但是要在应用程序置于后台甚至被用户杀死时维护数据,shared_preferences似乎是解决之道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM