set_state_4_test.dart 1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8

class Changer extends StatefulWidget {
9
  const Changer({ super.key });
10
  @override
11
  ChangerState createState() => ChangerState();
12 13 14 15 16 17 18 19
}

class ChangerState extends State<Changer> {
  void test0() { setState(() { }); }
  void test1() { setState(() => 1); }
  void test2() { setState(() async { }); }

  @override
Ian Hickson's avatar
Ian Hickson committed
20
  Widget build(BuildContext context) => const Text('test', textDirection: TextDirection.ltr);
21 22 23 24
}

void main() {
  testWidgets('setState() catches being used with an async callback', (WidgetTester tester) async {
25
    await tester.pumpWidget(const Changer());
26
    final ChangerState s = tester.state(find.byType(Changer));
27 28 29
    expect(s.test0, isNot(throwsFlutterError));
    expect(s.test1, isNot(throwsFlutterError));
    expect(s.test2, throwsFlutterError);
30 31
  });
}