Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Operating Systems II - Lab 7
ListView.builder, Debugging, and Web Services & APIsLazy lists, breakpoint debugging, and consuming REST APIs from Flutter
itemCount should always be tied to the actual list length, never a hard-coded numberitemCount throws a RangeError , the classic index-out-of-range crashListView.builder(
itemCount: courses.length,
itemBuilder: (context, index) => CourseCard(course: courses[index]),
)itemCount bound to courses.length, never a literal.StatelessWidget rather than building it inline// course_card.dart
class CourseCard extends StatelessWidget {
final String name;
const CourseCard({super.key, required this.name});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(title: Text(name)),
);
}
}Red screen
or error
Open Run tab
Find the exception message
Find file : line in the stack trace
Click to open that file
Identify the null or bad value
| Error text | Likely cause |
|---|---|
The method was called on null | A variable is null before a method/getter is called on it |
RangeError: Invalid value ... index | itemCount is larger than the list length |
type 'Null' is not a subtype of type 'String' | A field expected to be non-null arrived as null from JSON |
breakpoint (red dot)| Button | Action |
|---|---|
| Resume | Continue until the next breakpoint |
| Step Over | Execute the current line and move to the next |
| Step Into | Enter the method called on this line |
| Step Out | Finish the current method and return to the caller |
Worked Example
user.pointspoints arrives as nullpoints; confirm it is null in the Variables panelCtrl+Click) to locate where points is assignedpoints is missing from the JSON, the bug is in the API response, not the Dart code| Web Service | API | |
|---|---|---|
| Must be on the web | Yes | No |
| Has a standard (REST, SOAP) | Yes | Not required |
| Data format | Usually JSON or XML | Any |
Client
app, browser, Postman
Server
hosts the data or service
Flutter, that's the http packagehttp Packagehttp to pubspec.yaml, then run flutter pub gethttp.get() is asynchronous , always await itstatusCode before using the bodyfinal response = await http.get(Uri.parse('https://dummyjson.com/posts'));
if (response.statusCode == 200) {
final decoded = json.decode(response.body) as Map<String, dynamic>;
}http.get() returns a Future<Response>.fromJson factoryMap into a typed Dart model object.factory PostModel.fromJson(Map<String, dynamic> json) {
return PostModel(
id: json['id'] as int,
title: json['title'] as String,
);
}Loading
Success
Error
initState, never from inside build , build can run many times and would refire requests endlessly@override
void initState() {
super.initState();
_loadPosts();
}ListView.builder renders lazily; always bind itemCount to the real list lengthhttp package plus a fromJson factory is the standard way to fetch and parse REST data in FlutterinitStateMahmoud Abas|Lab 07: ListView.builder, Debugging, and Web Services & APIs