Record optimizations used according to Lao Meng’s blog

  1. FutureBuilder

Initialize the Future method in initState instead of passing the method directly

Error:

@override
Widget build(BuildContext context) {
  return FutureBuilder(
    future: httpCall(),
    builder: (context, snapshot) {
    },
  );
}
Copy the code

Optimization:

class _ExampleState extends State<Example> { Future<int> future; @override void initState() { future = Future.value(42); super.initState(); } @override Widget build(BuildContext context) { return FutureBuilder( future: future, builder: (context, snapshot) { }, ); }}Copy the code
  1. Bottom navigation switch page IndexedStack instead of switch method toggle – can cache pages.