在构建Builder时引发了以下断言:在构建过程中调用了setState()或markNeedsBuild()?

时间:2020-09-19 18:51:15

标签: mysql flutter

是新手,但实际上我看不出代码中的错误是什么,它首先一直显示: 构建Builder时引发了以下断言: 在构建期间调用setState()或markNeedsBuild()。 当我热沉闷的时候 4016行pos 12:'!_debugLocked': 是不正确的。 我不知道该怎么办,为什么会这样,因为这是第一次


class _RepresentativeUiState extends State<RepresentativeUi> {
  int _currentIndex = 0;
  // navy bottom pages
  final _newsFeedPage = RNewsFeed();
  final _profilePage = RProfile();
  final _addPostPage = RAddPost();
  final _libraryPage = RLibrary();
  Widget _showPage = RNewsFeed();
  Widget _pageChooser(int page) {
    switch (page) {
      case 0:
        return _newsFeedPage;
        break;
      case 1:
        return _addPostPage;
        break;
      case 2:
        return _libraryPage;
        break;
      case 3:
        return _profilePage;
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: CurvedNavigationBar(
        index: _currentIndex,
        items: <Widget>[
          Icon(
            FontAwesomeIcons.newspaper,
            size: 30,
          ),
          Icon(
            FontAwesomeIcons.plus,
            size: 30,
          ),
          Icon(
            FontAwesomeIcons.book,
            size: 30,
          ),
          Icon(
            FontAwesomeIcons.user,
            size: 30,
          ),
        ],
        color: Colors.white,
        buttonBackgroundColor: Colors.white,
        backgroundColor: Color(0xff131535),
        animationCurve: Curves.easeIn,
        animationDuration: Duration(milliseconds: 400),
        onTap: (int tappedIndex) {
          setState(() {
            _showPage = _pageChooser(tappedIndex);
          });
        },
      ),
      body: Container(
        child: _showPage,
      ),
    );
  }
}



class RNewsFeed extends StatefulWidget {
  @override
  _RNewsFeedState createState() => _RNewsFeedState();
}

class _RNewsFeedState extends State<RNewsFeed> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('1'),
    );
  }
}

class RAddPost extends StatefulWidget {
  @override
  _RAddPostState createState() => _RAddPostState();
}

class _RAddPostState extends State<RAddPost> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('2'),
    );
  }
}

class RLibrary extends StatefulWidget {
  @override
  _RLibraryState createState() => _RLibraryState();
}

class _RLibraryState extends State<RLibrary> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text('3'),
    );
  }
}

class RProfile extends StatefulWidget {
  @override
  _RProfileState createState() => _RProfileState();
}


class _RProfileState extends State<RProfile> {
  String _name ;
@override
  void initState() {
    super.initState();
    SharedPreferences.getInstance().then((value) => {
      setState((){
        value.getString('name');
      })
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff131535),
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              CircleAvatar(
                radius: 50,
              ),
              Text(
                '$_name',
                style: TextStyle(color: Colors.black),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

这是我认为的罪魁祸首:

SharedPreferences.getInstance().then((value) => {
  setState((){
    value.getString('name');
  })
});

无需在setState()中呼叫initState()。所以现在发生的是,在构建页面时,setState()正在尝试启动另一个构建过程。只需将value.getString('name');周围的setState包装器远程即可。