Complete the code to create a Flutter widget that displays a simple text.
class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Center(child: Text([1])); } }
The Text widget requires a string inside quotes to display text properly.
Complete the code to navigate to a new screen called DetailScreen when a button is pressed.
ElevatedButton(
onPressed: () {
Navigator.of(context).[1](MaterialPageRoute(builder: (_) => DetailScreen()));
},
child: Text('Go')
)Navigator.push adds a new screen on top of the current one, which is the correct way to navigate forward.
Fix the error in the code to correctly implement a repository interface in clean architecture.
abstract class UserRepository { Future<User> [1](int id); }
Using getUser is a common and clear method name for fetching a user by id in repository interfaces.
Fill in the blank to create a use case class that calls a repository method and returns a user.
class GetUserUseCase { final UserRepository repository; GetUserUseCase(this.repository); Future<User> execute(int id) async { return await repository.[1](id); } }
The repository method getUser is called with id.
Fill in the blanks to define a Flutter widget that uses a use case to fetch and display user data asynchronously.
class UserWidget extends StatefulWidget { @override _UserWidgetState createState() => _UserWidgetState(); } class _UserWidgetState extends State<UserWidget> { late Future<User> userFuture; final GetUserUseCase useCase = GetUserUseCase(UserRepositoryImpl()); @override void initState() { super.initState(); userFuture = useCase.[1](1); } @override Widget build(BuildContext context) { return FutureBuilder<User>( future: userFuture, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return CircularProgressIndicator(); } else if (snapshot.hasError) { return Text('Error'); } else { return Text(snapshot.data?.{{BLANK_3}} ?? 'No user'); } }, ); } }
The use case method execute is called with id 1. The user's name property is displayed.