Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

performance - Flutter: Can one split FutureBuilder build method from the parent Widget's build function?

Flutter recommends against splitting up a long build method into smaller methods, as Flutter uses the build method to optimize building its widget tree.

I'm curious whether this affects a FutureBuilder widget's builder function too. On the one hand, I'm thinking that builder is already a function by its nature, so it should be alright to split off into a separate method inside the parent widget, like so:

class MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    Future future = MyFuture();

    return Scaffold(
      body: FutureBuilder(
        future: future,
        builder: myBuilderMethod,
      ));
  }

  Widget myBuilderMethod(BuildContext builder, AsyncSnapshot snapshot) {
    if (snapshot.hasData) {
      ...
    }
  }
}

On the other hand, I'm not entirely sure how Flutter builds its widget tree here, so I'm scared I might be incurring some performance penalties compared to the standard inline version:

class MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    Future future = MyFuture();

    return Scaffold(
      body: FutureBuilder(
        future: future,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            ...
          }
        },
      ));
  }
}

Is it alright performance-wise to split off my builder method as shown in my first example?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...