set_state_5_test.dart 829 Bytes
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_test/flutter_test.dart';
6
import 'package:flutter/widgets.dart';
7 8 9

class BadWidget extends StatefulWidget {
  @override
10
  State<StatefulWidget> createState() => BadWidgetState();
11 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 30
  }
}

void main() {
  testWidgets('setState() catches being used inside a constructor', (WidgetTester tester) async {
31
    await tester.pumpWidget(BadWidget());
32
    expect(tester.takeException(), isInstanceOf<FlutterError>());
33 34
  });
}