set_state_5_test.dart 841 Bytes
Newer Older
1 2 3 4 5
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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 10 11 12 13 14 15 16 17 18 19

class BadWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new BadWidgetState();

}

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

21 22 23 24 25 26 27 28 29 30 31
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return new Text(_count.toString());
  }
}

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