【问题标题】:How to save data from screen to separated class and views in another screen in flutter?如何将数据从屏幕保存到单独的类和另一个屏幕中的视图?
【发布时间】:2020-07-03 17:56:09
【问题描述】:

我的问题是我现在可以将随机数据保存在单独类中的列表中,并通过单击平面按钮将其显示在同一屏幕上,但是当我移至第二个屏幕时,我想查看列表中的相同数据我收到了一个错误,因为列表是空的!!

这是将数据发送到列表并在平面按钮上显示的页面

    WordsSaved wordSaved = WordsSaved();
    TestMemory testMemory = TestMemory();

    class WordsCard extends StatefulWidget {

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

    class _WordsCardState extends State<WordsCard> {
  int i=0;
  int _random;
  int get random => _random;
  String word ;
  int numberOfWords = 5;

  @override

  Widget build(BuildContext context) {
    _random = Random().nextInt(arabicWords.length);

    double currentOpacity = 1;
    return
      Scaffold(
        backgroundColor: Colors.white,
        body:
                  FlatButton(
                    padding: EdgeInsets.all(35),
                    onPressed: ()=> {
                      setState(() {
                        if(wordSaved.wordsSaved.length < numberOfWords)
                          {
                        wordSaved.wordsSaved.add(arabicWords[_random]);
                        i++;
                          }
                        else
                          {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) => TestMemory()
                            ),
                            );
                            print('The Words are Finished');
                          }
                          },
                        ),
                    },
                    child:
                    AutoSizeText(
                      wordSaved.wordsSaved[i],
                      style: TextStyle(color: Colors.white, fontSize: 50),
                      textAlign: TextAlign.center,
                      maxLines: 2,
                    ),
                  ),
),
}

这是单独的类


class WordsSaved {

  WordsSaved({ this.word}) ;
  final String word;
  int _random;
  int get random => _random;

  List<String> wordsSaved = [];
}

这是从列表中查看相同数据的声音屏幕


WordsSaved wordSaved = WordsSaved();

class TestMemory extends StatefulWidget {
  @override
  _TestMemoryState createState() => _TestMemoryState();
}

class _TestMemoryState extends State<TestMemory> {

  int i=0;

  @override
  Widget build(BuildContext context) {
    return
      Scaffold(
        backgroundColor: Colors.white,
        body:
             FlatButton(
                    padding: EdgeInsets.all(35),
                    onPressed: ()=> {
                      setState(() {

                      },
                      ),
                    },
                    child:
                    AutoSizeText(
                      wordSaved.wordsSaved[i],
                      style: TextStyle(color: Colors.white, fontSize: 50),
                      textAlign: TextAlign.center,
                      maxLines: 2,
                    ),
                  ),
)
}

【问题讨论】:

  • 您可能想查看您的单词保存类的 Get_It 包(Singleton)。访问从第一个屏幕保存的单词以保存数据。访问从第二个屏幕保存的单词以检索数据。 locator() 是你的朋友。

标签: list flutter


【解决方案1】:

欢迎来到 Stack Overflow,感谢您的提问!您可以使用provider 包来处理应用程序不同页面中WordsSaved 类的状态。我在下面添加了一个代码示例,向您展示了如何做到这一点。如果您尚未查看文档here,您可能想查看有关如何在 Flutter 应用程序中处理状态的更多信息。

import 'dart:math';

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

void main() {
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(
        create: (context) {
          return WordsSaved();
        },
      ),
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WordsCard(),
    );
  }
}

class WordsSaved extends ChangeNotifier {
  final _wordsSaved = <String>[''];

  List<String> get wordsSaved => List.unmodifiable(_wordsSaved);

  void addWord(String word) {
    _wordsSaved.add(word);
    notifyListeners();
  }

  void removeWord(String word) {
    _wordsSaved.remove(word);
    notifyListeners();
  }
}

class WordsCard extends StatefulWidget {
  @override
  _WordsCardState createState() => _WordsCardState();
}

class _WordsCardState extends State<WordsCard> {
  final arabicWords = <String>[
    'Aamer',
    'Al-Abadi',
    'Abargil',
    'Abaza',
    'Abbar',
  ];

  int i = 0;
  int _random;
  int numberOfWords = 5;

  @override
  Widget build(BuildContext context) {
    _random = Random().nextInt(arabicWords.length);

    final wordsSaved = context.watch<WordsSaved>();

    return Scaffold(
      backgroundColor: Colors.white,
      body: FlatButton(
        padding: EdgeInsets.all(35),
        onPressed: () {
          if (wordsSaved.wordsSaved.length < numberOfWords) {
            wordsSaved.addWord(arabicWords[_random]);

            setState(() {
              i++;
            });
          } else {
            Navigator.of(context).push(
              MaterialPageRoute(
                builder: (context) => TestMemory(),
              ),
            );
          }
        },
        child: Text(
          wordsSaved.wordsSaved[i],
          style: TextStyle(color: Colors.white, fontSize: 50),
          textAlign: TextAlign.center,
          maxLines: 2,
        ),
      ),
    );
  }
}

class TestMemory extends StatelessWidget {
  final i = 0;

  @override
  Widget build(BuildContext context) {
    final wordsSaved = context.watch<WordsSaved>();

    return Scaffold(
      backgroundColor: Colors.white,
      body: FlatButton(
        padding: EdgeInsets.all(35),
        onPressed: () {},
        child: Text(
          wordsSaved.wordsSaved[i],
          style: TextStyle(color: Colors.white, fontSize: 50),
          textAlign: TextAlign.center,
          maxLines: 2,
        ),
      ),
    );
  }
}

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 2019-05-10
    • 2019-07-18
    • 1970-01-01
    • 2010-11-09
    • 2021-04-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多