如何在将来的构建器(Flutter / Dart)中等待变量不等于null?

我有一个带有两个Dart文件的简单应用程序:main.dart和bukalapak.dart

出于演示目的,该应用程序只有两个Future Text()小部件。基本上,一个“文本”窗口小部件获取某个html的名称,另一个窗口小部件获取同一html的总计。不要问为什么,但是将来的“名称”构建器必须位于bukalapak.dart中的单独的有状态小部件中。我的问题是我如何才能等到html不为null,然后显示总的Text小部件,因为我可以轻松地再次调用url,但这将完成两倍的工作。我只想调用一次http.get。

这是main.dart的代码:

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    Bukalapak bukalapak = Bukalapak();

    return Scaffold(
      appBar: AppBar(
        title: Text('data'),
      ),
      body: Container(
        child: Column(
          children: <Widget>[
            RandomWidget(
              bukalapak: bukalapak,
            ),
            FutureBuilder(
              builder: (context, snapshot) {
                return Container(
                  color: Colors.grey,
                  height: 28.0,
                  padding: EdgeInsets.only(left: 20.0),
                  child: Align(
                      alignment: Alignment.centerLeft,
                      child: Text('Total results: ${snapshot.data}')),
                );
              },
              future: bukalapak.getTotal(),
            )
          ],
        ),
      ),
    );
  }
}

bukalapak.dart的代码:

class Bukalapak {
  var html;

  Future<dynamic> getTotal() async {
    // wait until html != null, then perform this
    var a = html.querySelectorAll(
        '#display_product_search > div.product-pagination-wrapper > div.pagination > span.last-page');
    dynamic total = int.parse(a[0].text) * 50;
    total = '$total'.replaceAllMapped(
        new RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (Match m) => '${m[1]},');
    return total;
  }

  Future<dynamic> getName() async {
    http.Response response = await http.get(
        'https://www.bukalapak.com/products/s?from=omnisearch&from_keyword_history=false&page=0&search%5Bkeywords%5D=paper&search_source=omnisearch_organic&source=navbar&utf8=✓');

    if (response.statusCode == 200) {
      String data = response.body;
      html = parse(data);

      var nameElement = html.querySelector(
          'li.col-12--2 > div.product-card > article > div.product-media > a');
      String title = nameElement.attributes['title'];
      return title;
    } else {
      throw Exception('Bukalapak error: statusCode= ${response.statusCode}');
    }
  }
}

class RandomWidget extends StatefulWidget {
  RandomWidget({this.bukalapak});
  final Bukalapak bukalapak;
  @override
  _TextState createState() => _TextState();
}

class _TextState extends State<RandomWidget> {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      builder: (context, snapshot) {
        return Container(
          color: Colors.grey,
          height: 28.0,
          padding: EdgeInsets.only(left: 20.0),
          child: Align(
              alignment: Alignment.centerLeft,
              child: Text('Name results: ${snapshot.data}')),
        );
      },
      future: widget.bukalapak.getName(),
    );
  }
}