2

Im working on a project with the OGD of the Austrian public transportation API which wants for a request just a getter with a "stopID". Also you can concatenate more than one stopID. That means if you try this link, you get a response with all data of the stopIDs:

http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=2913&stopId=2918&stopId=2921&stopId=3387&stopId=4133&stopId=4134

And now here is my problem: The stopID with the number 4133 (try it on http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133) has NULL values in the coordinates. Normally I would just do some NULL checks but there is some strange behaviour I've never seen. I've debugged to the point of the error. It's at the HTTP.get Request and says:

I/flutter (29464): ERROR: Invalid argument(s) (input): Must not be null 

But how is that possible if I havent gotten the response at this point? It tries to request and breaks somewhere at building the response.

This is the request code for it (finalURl is the URL from above):

    final Response response = await http.get(finalUrl);
    if (response.statusCode == 200) {
      final encode = jsonDecode(response.body);
}

The error happens even before the if statement and parsing the json String. So before it gets a proper Response Object. If you ask now how I know why it happens because of the NULL types in the coordinate fields, I've tried the Request without the ID 4133 and it worked fine. If I just use the second request Link (with only the ID 4133), it throws the error. Does anybody have an idea whats wrong there? It is definetly a problem with Dart/Flutter, did I miss something?

3
  • Can you please attach the full stacktrace? Commented Jul 19, 2020 at 13:14
  • @julemand101 Thats the weird part. There is no stacktrace, just this line pops up and the app stops running. Commented Jul 19, 2020 at 13:34
  • Can you make a small example which reproduce the issue which you can attach to your question? Commented Jul 19, 2020 at 13:35

2 Answers 2

2

I am not sure how are you importing the http package. If you are using "http" alias to call get() make sure you import as follows,

import 'package:http/http.dart' as http;

and also use the Response class from the package as

http.Response;

I tried to get the data from the given url, it worked for me.

...
import 'package:http/http.dart' as http;
...

Future<void> fetchData() async {

    const url = "http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133";

    final http.Response response = await http.get(url);
    final data = json.decode(response.body) as Map<String, dynamic>;
    print(data);

}

Alternatively you can skip the class and let Dart infer the response type as follows:

...
import 'package:http/http.dart' as http;
...

Future<void> fetchData() async {

    const url = "http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133";

    final response = await http.get(url);
    final data = json.decode(response.body) as Map<String, dynamic>;
    print(data);

}

Debug Console:

I/flutter (20609): {data: {monitors: [{locationStop: {type: Feature, geometry: {type: Point, coordinates: [null, null]}, properties: {name: 60200949, title: Oberlaa, municipality: Wien, municipalityId: 90001, type: stop, coordName: WGS84, gate: 2, attributes: {rbl: 4133}}}, lines: [{name: U1, towards: NICHT EINSTEIGEN ! NO DEPARTURE PLATFORM, direction: R, platform: 2, richtungsId: 2, barrierFree: true, realtimeSupported: true, trafficjam: false, departures: {departure: [{departureTime: {}, vehicle: {name: U1, towards: NICHT EINSTEIGEN ! NO DEPARTURE PLATFORM, direction: R, richtungsId: 2, barrierFree: false, realtimeSupported: true, trafficjam: false, type: ptMetro, attributes: {}, linienId: 301}}]}, type: ptMetro, lineId: 301}], attributes: {}}]}, message: {value: OK, messageCode: 1, serverTime: 2020-07-19T15:55:30.000+0200}}

You can then do null checking on the data. Also try putting code in a try catch block wherever you are calling your fetchData() funtion to handle errors properly.

Sign up to request clarification or add additional context in comments.

2 Comments

I found the problem after 2 hours getting over all. The problem was not the get request, the problem was my model, which handled the departure time wrong at empty departure lists. However, it gets me that the debugger stopped everytime after the get request and never passed the if statement to check the statusCode
Glad you found the solution :D
0

I do my gets like this and add ?? '' to any string value that might come back as a NULL. and use ?? 0 for integers.

  Widget _loadPage() {
  Future<List<JsonHomeMembers>> getMembers() async {
  var data = await http
      .get(finalUrl);
  var dataDecoded = json.decode(data.body);
  _updateMessage = dataDecoded.toString();

  List<JsonHomeMembers> posts = List();

  dataDecoded.forEach(
    (post) {
      int memberId = post["member_id"] ?? 0;
      String memberUserName = post["member_username"];
      String memberJoinDate = post["member_join_date"];
      String memberRole = post["member_role_text"] ?? '';

      posts.add(
        JsonHomeMembers(
            memberId,
            memberUserName,
            memberJoinDate,
            memberRole),
      );
    },
  );
  return posts;
}
return Container(
  child: FutureBuilder(
    future: showPosts(),
    builder: (context, snapshot) {
      if (snapshot.hasData) {
        return ListView.builder(
          shrinkWrap: true,
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Text(
                  snapshot.data[index].memberUserName, style: TextStyle(fontSize: 16, fontWeight: FontWeight.w200, color: Colors.white),
                ),
                Text(
                  snapshot.data[index].memberJoinDate, style: TextStyle(fontSize: 16, fontWeight: FontWeight.w200, color: Colors.white),
                ),
                Text(
                  snapshot.data[index].memberRole, style: TextStyle(fontSize: 16, fontWeight: FontWeight.w200, color: Colors.white),
                ),
              ],
            );
          },
        );
      } else {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
               Text(
                    'Data not available! Check your internet connection!',
                    style: globals.blueGrey_20_700,
                  ),
          ],
        );
      }
    },
  ),
);

}

1 Comment

Yeah, that will work for sure. My problem is, that the error comes earlier before i can check if the value is null. Even before the json parsing happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.