如果数据库存在抖动,则执行

时间:2019-03-08 14:19:34

标签: flutter

所以基本上我想做的是:

当用户首次打开应用程序时, 创建SQLite数据库,并从Internet上获取数据。 在完成此操作之前,SetupPage()小部件将显示在支架主体中,否则将显示Home()。

现在,我编写的代码第一次可以完美运行,但是当我第二次打开它时, SetupPage()仅显示,它永远不会返回到Home()。我在这里做错了什么?

import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart';
import 'package:path/path.dart';
import 'pages/home.dart';
import 'pages/SetUpPage.dart';
import 'package:sqflite/sqflite.dart';

class App extends StatefulWidget {
  createState() {
    return AppState();
  }
}

class AppState extends State<App> {
  final bgColor = const Color(0xFF1abc9c);
  final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

  bool status = false;

  Database database;

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

  void initializeData() async
  {
    var databasesPath = await getDatabasesPath();
    String path = join(databasesPath, 'demo.db');
    status = false;
    Database database = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {

      await db.execute(
          'CREATE TABLE news (id INTEGER PRIMARY KEY, topic TEXT, img TEXT, newstitle TEXT, news TEXT, newslink TEXT)');

      fetchData();
    }
    );

    database.close();
  }
  @override
  Widget build(context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: AppBar(
        title: Text("UnFound News"),
        backgroundColor: bgColor,
      ),

      body: status ? Home() : SetUpPage(),
    );
  }

  /*Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );*/

  void fetchData() async {
    var result = await get("https://api.myjson.com/bins/a0bvu");
    var arr = json.decode(result.body)['post'];

    for(int i = 0; i < arr.length; i++)
    {
      //TODO: Implement addition to database.
    }

    setState(() {
      status = true;
    });
  }
}

2 个答案:

答案 0 :(得分:0)

您可能希望将fetchData放在onOpen参数中,而不是像这样的onCreate参数:

Database database = await openDatabase(path, version: 1,
     onCreate: (Database db, int version) async {

         await db.execute(
              'CREATE TABLE news (id INTEGER PRIMARY KEY, topic TEXT, img TEXT, newstitle TEXT, news TEXT, newslink TEXT)');

         await fetchData();

    }, onOpen: (Database db) async {
        setState(() {
            status = true;
        });
    });

答案 1 :(得分:0)

persoane(0, []) :- ! .
persoane(N, [(_Men, _Color, _Drink, _Smoke, _Animal)|T]) :- N1 is N-1, persoane(N1,T) .

persoana(1, [H|_], H) :- ! .
persoana(N, [_|T], R) :- N1 is N-1, persoana(N1, T, R)

% Function that tests if A is to the right of B (will be used in the final question)
right(A, B, List) :- nextto(B, A, List).

% The Brit lives in the red house
hint1([(brit, red, _, _, _)|_]) .
hint1([_|T]) :- hint1(T) .

% The Swede keeps dogs as pets
hint2([(swede, _, _, _, dog)|_]) .
hint2([_|T]) :- hint2(T) .

% The Dane drinks tea
hint3([(dane, _, tea, _, _)|_]) .
hint3([_|T]) :- hint3(T) .

% The green house is on the left of the white house
hint4([(_, green, _, _, _,),(_, white, _, _, _)|_]) .
hint4([_|T]) :- hint4(T) .

% The green house owner drinks coffee
hint5([(_, green, cofee, _, _)|_]) .
hint5([_|T]) :- hint5(T) .

% The person who smokes Pall Mall rears birds
hint6([(_, _, _, pallmall, bird)|_]) .
hint6([_|T]) :- hint6(T) .

% The owner of the yellow house smokes Dunhill
hint7([(_, yellow, _, dunhill, _)|_]) .
hint7([_|T]) :- hint7(T) .

% The man living in the center house drinks milk
hint8(persoane) :- persoana(3, persoane, (_, _, milk, _, _)) .

% The Norwegian lives in the first house
hint9(persoane) :- persoana(1, persoane, (norwegian, _, _, _, _)) .

% The man who smokes Blends lives next to the one who keeps cats
hint10([(_, _, _, blend, _),(_, _, _, _, cat)|_]) .
hint10([(_, _, _, _, cat),(_, _, _, blend, _)|_]) .
hint10([_|T]) :- hint10(T) .

% The man who keeps horses lives next to the man who smokes Dunhill
hint11([(_, _, _, dunhill, _),(_, _, _, _, horse)|_]) .
hint11([(_, _, _, _, horse),(_, _, _, dunhill, _)|_]) .
hint11([_|T]) :- hint11(T) .

% The owner who smokes BlueMaster drinks beer
hint12([(_, _, beer, bluemaster, _)|_]) .
hint12([_|T]) :- hint12(T) .

% The German smokes Prince
hint13([(german, _, _, prince, _)|_]) .
hint13([_|T]) :- hint13(T) .

% The Norwegian lives next to the blue house
hint14([(norwegian, _, _, _, _),(_, blue, _, _, _)|_]) .
hint14([(_, blue, _, _, _),(norwegian, _, _, _, _)|_]) .
hint14([_|T]) :- hint14(T) .

% The man who smokes Blend has a neighbour who drinks water
hint15([(_, _, _, blend, _),(_, _, water, _, _)|_]) .
hint15([(_, _, water, _, _),(_, _, _, blend, _)|_]) .
hint15([_|T]) :- hint15(T) .

% The question: What pet does the man who lives to the right of the red house have ?
% question (right((_, _, _, _, _), (_, red, _, _, _), persoane)) .
question([(_, red, _, _, _),()])
question([_|T]) :- question(T) .

solution(persoane) :-
   persoana(5, persoane),
   hint1(persoane),
   hint2(persoane),
   hint3(persoane),
   hint4(persoane),
   hint5(persoane),
   hint6(persoane),
   hint7(persoane),
   hint8(persoane),
   hint9(persoane),
   hint10(persoane),
   hint11(persoane),
   hint12(persoane),
   hint13(persoane),
   hint14(persoane),
   hint15(persoane),
   question(persoane) .