多次打开页面的错误

时间:2019-04-29 04:51:06

标签: dart flutter flutter-layout

将来的构建器将运行多次并同时打开多个页面。如何解决此错误?

SubCategory StatefulWidget

class _mainCategory extends State<subCategory> {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: submitRequestAppBar(context),
      body: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.grey[350],
            leading: Container(),
            title: Text(
              widget.title,
              textAlign: TextAlign.left,
              style:  TextStyle(fontWeight: FontWeight.bold, color: Colors.black),),),

_mainCategory状态小部件

          body: FutureBuilder(
            future: getRegister1(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text('Press button to start.');
                case ConnectionState.active:
                case ConnectionState.waiting:
                  return waitingCircle();
                case ConnectionState.done:
                  if (snapshot.hasError) return Text('Error: ${snapshot.error}');
                  if (snapshot.data.length == 0)
                    return noResultFound();
                  else
                    return createListView(context, snapshot); }
              return null; // unreachable
            },)),); }

体内的FutureBuilder

  Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<SubCategoryItem> values = snapshot.data;

CreateListView窗口小部件

    return ListView.builder(
      padding: EdgeInsets.only(top: 8.0, right: 0.0, left: 0.0),
      itemCount: 1,
      itemBuilder: (BuildContext context, int index) {
        return GridView.count(
          physics: ScrollPhysics(),
          shrinkWrap: true,
          crossAxisCount: 4,

          children: List.generate(values.length, (index) {

返回ListView

            return GridTile(
              child: GestureDetector(
                onTap: () => Navigator.push(
                    context,
                    MaterialPageRoute(
                        builder: (context) => WoDescription2(
                            widget.RegId,
                            widget.AssetId,
                            widget.ParentId,
                            values[index].childId,
                            false,
                            1,
                            widget.title,
                        widget.equipmentId))),

返回GridTile

                child: Column(
                  children: [
                    Card(
                      child: Container(
                        decoration: BoxDecoration(
                            border: Border.all(
                                color: Colors.blueAccent, width: 1.5)),
                        child: Stack(
                          children: <Widget>[

列小部件

                            SvgPicture.asset('assets/images/Defect/icon-${values[index].childId}.svg',
                              height: 50.0,),],),), ),
                    Expanded(
                      child: Text(
                        '${values[index].description}',
                        textAlign: TextAlign.center,
                        style: TextStyle(fontSize: 10.0),),),],),), ); }),); },);}

flutter_svg插件用于svg图像

  Future getRegister1() async {
    List<SubCategoryItem> description = [];
    List cat = [];

    var catLocal = (await HelperDatabase1().displayDefCatRelation());
    var defCatLocal = (await HelperDatabase1().display());

    for (int i = 0; i < catLocal.length; i++) {
      if (widget.RegId == catLocal[i].r &&
          widget.AssetId == catLocal[i].t &&
          widget.ParentId == catLocal[i].p) {
        cat.add(catLocal[i].c);}}

    for (int i = 0; i < cat.length; i++) {
      for (int j = 0; j < defCatLocal.length; j++) {
        if (cat[i] == defCatLocal[j].deF_CAT_ID) {

          var oneItem = SubCategoryItem(
              childId: defCatLocal[j].deF_CAT_ID,
              description: defCatLocal[j].description);
          await description.add(oneItem);}}}
    return description;}}

FutureBuider的未来:getRegister1

class SubCategoryItem {
  int childId;String description;
  SubCategoryItem({this.childId, this.description});}

SubCategoryItem类

library(dbplyr) #in case you have an error, run: system("defaults write org.R-project.R force.LANG en_US.UTF-8")  from Rails console, then restart R.
library(processx)
library(RPostgres)
library(httr)
library(tidyverse)
library(dplyr)

config <- run("heroku", c("config:get", "postgres://xxxxxxxxxxxxxx
", "-a", "prjectAlpha"))
pg <- httr::parse_url(config$stdout)
dbConnect(RPostgres::Postgres(),
          dbname = "xxxxxxxxxx",
          host = "xxxxxxxxx.amazonaws.com",
          port = 5432,
          user = "xxxxxx",
          password = "xxxxxxxxxxxxxxxxx",
          sslmode = "require"
) -> db_con

enter image description here

1 个答案:

答案 0 :(得分:0)

只需在initState上声明Future的Build方法/

_mainCategory状态小部件

class _mainCategory extends State<subCategory> {
  Future _futureData;

  @override
  void initState() {
      super.initState();    
      _futureData = getRegister1();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(

体内的FutureBuilder

body: FutureBuilder(
  future: _futureData,
     builder: (BuildContext context, AsyncSnapshot snapshot) {