MVVM stands for Model-View-ViewModel. It is a design pattern that separates the app's data (Model), UI (View), and the logic that connects them (ViewModel).
Click to reveal answer
beginner
What is the role of the ViewModel in MVVM?
The ViewModel holds the app's logic and state. It communicates with the Model to get data and prepares it for the View. It also handles user actions from the View.
Click to reveal answer
intermediate
How does the View communicate with the ViewModel in Flutter MVVM?
The View listens to changes in the ViewModel using state management tools like ChangeNotifier or Streams. When the ViewModel updates, the View rebuilds to show new data.
Click to reveal answer
beginner
Why is MVVM useful in Flutter app development?
MVVM helps keep code organized by separating UI from business logic. This makes the app easier to test, maintain, and scale as the UI and logic are independent.
Click to reveal answer
intermediate
In MVVM, where should API calls be made?
API calls should be made in the Model or a service class. The ViewModel calls these services to get data and then updates the View accordingly.
Click to reveal answer
In MVVM, which component is responsible for displaying the UI?
AView
BModel
CViewModel
DController
✗ Incorrect
The View is responsible for displaying the UI and reacting to user input.
Which Flutter class is commonly used to notify the View about changes in the ViewModel?
AStatelessWidget
BInheritedWidget
CFutureBuilder
DChangeNotifier
✗ Incorrect
ChangeNotifier is used to notify listeners, typically the View, about changes in the ViewModel.
Where should business logic be placed in MVVM?
AView
BViewModel
CWidget
DModel
✗ Incorrect
The ViewModel contains the business logic and state management.
What is the main benefit of separating View and ViewModel?
AEasier testing and maintenance
BFaster UI rendering
CMore animations
DLess code
✗ Incorrect
Separating View and ViewModel makes the app easier to test and maintain.
In Flutter MVVM, how does the ViewModel update the View?
ABy directly modifying UI widgets
BBy calling setState() in the View
CBy notifying listeners through ChangeNotifier or Streams
DBy rebuilding the entire app
✗ Incorrect
The ViewModel notifies the View using ChangeNotifier or Streams, prompting the View to rebuild.
Explain the roles of Model, View, and ViewModel in the MVVM pattern in Flutter.
Think about who does what in the app: data, UI, and logic.
You got /4 concepts.
Describe how state management helps implement MVVM in Flutter.
Focus on how the View learns about data changes.
You got /4 concepts.
Practice
(1/5)
1. What is the main role of the ViewModel in the MVVM pattern in Flutter?
easy
A. To handle user input events directly from the UI
B. To hold data and business logic, notifying the View of changes
C. To display the user interface elements on the screen
D. To manage navigation between different screens
Solution
Step 1: Understand MVVM roles
The ViewModel acts as a middleman holding data and logic separate from UI.
Step 2: Identify ViewModel responsibility
It notifies the View when data changes so UI updates automatically.
Final Answer:
To hold data and business logic, notifying the View of changes -> Option B
Quick Check:
ViewModel = Data + Logic holder [OK]
Hint: ViewModel holds data and logic, not UI display [OK]
Common Mistakes:
Confusing ViewModel with View
Thinking ViewModel handles UI rendering
Assuming ViewModel manages navigation
2. Which of the following is the correct way to notify the View about data changes in a ViewModel using Flutter's ChangeNotifier?
easy
A. call notifyListeners() inside the ViewModel after data update
B. call setState() inside the ViewModel after data update
C. call build() method directly from the ViewModel
D. call dispose() method after data update
Solution
Step 1: Recall ChangeNotifier usage
ChangeNotifier requires calling notifyListeners() to update listeners.
Step 2: Identify correct notification method
notifyListeners() triggers UI rebuilds in listening Views.
Final Answer:
call notifyListeners() inside the ViewModel after data update -> Option A
Quick Check:
notifyListeners() updates View [OK]
Hint: Use notifyListeners() in ViewModel to update UI [OK]
Common Mistakes:
Using setState() inside ViewModel (wrong place)
Trying to call build() manually
Calling dispose() instead of notifyListeners()
3. Given this ViewModel code snippet in Flutter:
class CounterViewModel extends ChangeNotifier {
int _count = 0;
int get count => _count;
void increment() {
_count++;
notifyListeners();
}
}
What will be the value of count after calling increment() twice starting from 0?
medium
A. 0
B. 1
C. 2
D. Error: notifyListeners() not called
Solution
Step 1: Analyze initial value and increment
_count starts at 0, increment() adds 1 each call.
Step 2: Calculate after two increments
After two calls, _count = 0 + 1 + 1 = 2.
Final Answer:
2 -> Option C
Quick Check:
Two increments = count 2 [OK]
Hint: Each increment adds 1; two calls add 2 total [OK]
Common Mistakes:
Forgetting notifyListeners() updates UI but doesn't change count
Assuming count resets after notifyListeners()
Confusing initial value with updated value
4. Identify the error in this ViewModel code snippet:
class UserViewModel extends ChangeNotifier {
String _name = '';
String get name => _name;
void updateName(String newName) {
_name = newName;
}
}
Why won't the UI update when updateName is called?
medium
A. notifyListeners() is missing after updating _name
B. Getter method name is incorrect
C. ChangeNotifier should not be extended
D. updateName should be async
Solution
Step 1: Check data update method
updateName changes _name but does not notify listeners.
Step 2: Understand UI update trigger
Without notifyListeners(), UI won't rebuild on data change.
Final Answer:
notifyListeners() is missing after updating _name -> Option A
Quick Check:
Missing notifyListeners() blocks UI update [OK]
Hint: Always call notifyListeners() after data changes [OK]
Common Mistakes:
Forgetting notifyListeners() after state change
Thinking getter name causes UI update
Assuming async needed without async code
5. You want to build a Flutter app using MVVM where the View updates automatically when the ViewModel's list of items changes. Which approach correctly implements this behavior?
hard
A. Store the list in View and pass it to ViewModel without notifications
B. Directly modify the list in the View and call setState() in ViewModel
C. Use a StatefulWidget for ViewModel and call build() manually after list changes
D. Use a ChangeNotifier in ViewModel, call notifyListeners() after modifying the list, and use a Consumer widget in View
Solution
Step 1: Understand MVVM data flow
ViewModel holds data and notifies View on changes using ChangeNotifier.
Step 2: Identify correct Flutter pattern
Calling notifyListeners() after list changes triggers Consumer widget rebuild in View.
Step 3: Eliminate incorrect options
setState() belongs in View, not ViewModel; manual build() calls are incorrect; storing list only in View breaks MVVM.
Final Answer:
Use a ChangeNotifier in ViewModel, call notifyListeners() after modifying the list, and use a Consumer widget in View -> Option D