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

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

class BadWidget extends StatefulWidget {
9
  const BadWidget({ super.key });
10
  @override
11
  State<StatefulWidget> createState() => BadWidgetState();
12 13 14 15 16
}

class BadWidgetState extends State<BadWidget> {
  BadWidgetState() {
    setState(() {
17
      _count = 1;
18 19
    });
  }
20

21 22 23 24
  int _count = 0;

  @override
  Widget build(BuildContext context) {
25
    return Text(_count.toString());
26 27 28 29
  }
}

void main() {
30
  testWidgets('setState() catches being used inside a constructor', (WidgetTester tester) async {
31
    await tester.pumpWidget(const BadWidget());
Dan Field's avatar
Dan Field committed
32
    expect(tester.takeException(), isFlutterError);
33 34
  });
}