Complete the code to create a simple Flutter app with a centered text.
import 'package:flutter/material.dart'; void main() { runApp([1]); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Text('Hello, Dart!'), ), ), ); } }
The runApp function needs the root widget of the app, which is MyApp() here.
Complete the code to define a widget that updates its UI when a button is pressed.
class Counter extends StatefulWidget { @override _CounterState createState() => [1](); } class _CounterState extends State<Counter> { int count = 0; void increment() { setState(() { count++; }); } @override Widget build(BuildContext context) { return Column( children: [ Text('Count: $count'), ElevatedButton(onPressed: increment, child: Text('Add')), ], ); } }
The createState method must return an instance of the private state class _CounterState.
Fix the error in the code to properly update the UI when the button is pressed.
class Clicker extends StatefulWidget { @override _ClickerState createState() => _ClickerState(); } class _ClickerState extends State<Clicker> { int clicks = 0; void onClick() { [1](() { clicks++; }); } @override Widget build(BuildContext context) { return Column( children: [ Text('Clicks: $clicks'), ElevatedButton(onPressed: onClick, child: Text('Click me')), ], ); } }
To notify Flutter to rebuild the UI, you must call setState with a function that updates the state.
Fill both blanks to create a map of word lengths for words longer than 3 characters.
words = ['apple', 'bat', 'carrot', 'dog']; lengths = { for (var word in words) if (word.[1] [2] 3) word: word.[1] };
The dictionary comprehension maps each word to its length using word.length. The condition filters words with length greater than 3 using >.
Fill all three blanks to create a dictionary of uppercase keys and values filtered by value > 0.
data = {'a': 1, 'b': 0, 'c': 3};
result = { for (var kv in data.entries) if (kv.value [3] 0) [1]: [2] };The dictionary comprehension uses kv.key.toUpperCase() as keys, kv.value as values, and filters entries where kv.value > 0.