last day (15 days later) » 

2:00 PM
0
Q: _TypeError (type 'Null' is not a subtype of type 'String') when not acutally 'Null'

MackinaI am having an issue where as the title describes and the screenshot shows that one of my strings in my model is being considered 'Null', when it actually isn't. First screenshot: As shown in my second screenshot, I am printing the response from the API and the value in the key/value pair is ac...

 
Could you do: print(json) to check what are inside the parsed json object?
 
it just says 'Instance of JsonCodec'
 
You should print it somewhere where you actually have access to the json variable you are using in the screenshots. But you can also try call it something else since json are already something declared inside dart:convert.
 
I also did a print(json.decode(response.body)) and it gives me the full response with the value of the questionable key filled, which is what I would expect I guess. I am just perplexed as to why it would be considered null.
 
Well, we need more context to know for sure what is going on. You have basically not shown us any relevant code. E.g. where is json being defined and how? Post the full method if you can.
 
2:00 PM
Im not sure where I would do that as I am new to Dart. The screenshots above are in reference to my model file. I don't believe I can put a print statement there.
 
2:12 PM
I updated my question to show my data model file as well as my api services file
 
Great, I will take a look. Was answering some other question :)
 
Thats ok!
I appreciate your help and I can by all means be patient.
 
Do you think you could send me an example of the JSON you are parsing? If it contains private information, you can send me an e-mail and I promise to not making it public (julemand101@archlinux.dk)
 
I dont have a problem sharing the whole project'
 
Even better. But I just need the JSON :)
 
2:21 PM
I love programming and am striving to be the best I can be. The sad part is that I feel like I am on an island. I dont work with anyone that programs in Dart and therefore I have no one I can ask questions.
You want the API response?
in Json
 
Yeah
Just raw unparsed JSON
@Mackina Well, you are welcome to join the "The Unofficial Dart Community" Discord server or r/FlutterDev Discord server. :)
 
I will join it for sure
 
Programmed in Dart for 10 years so I have seen enough shit to probably help you :D
 
hahaha
thats awesome
I think you have helped me before
I sent the email to btw
 
probably... I don't remember names
I got your mail but got into another issue when trying to parse your JSON
Give me a few minutes to do an analysis
Ok, what shit have you used to generate this parser?
Are this code even meant for null-safety?
 
2:37 PM
sorry my wife calld
called*
 
It is fine. What is not fine is the code contains this:

dimensionDisplay: json["dimension_display"] == null
? null
: json["dimension_display"],

While the field are defined to be:

String dimensionDisplay;
 
I initially do the call in post man, I take the response and copy it to this website:
https://app.quicktype.io/
then I create my model file and paste it there
yeah it was auto generated
 
Great. Fun fact, that generator have not been updated for ages and does not make valid Dart code
 
Well, at least not valid Dart 2.12 code. We are at Dart 2.17 now
 
2:39 PM
but I get all sorts of errors there as well
lol
 
Dart 2.12 introduced the concept of null-safety. So String means a variable that can never be null. If null are possible, the type needs to be String?
 
understandable, but what about the instances like the one posed in my question.
gmtLastRecordDate: DateTime.parse(json["gmt_last_record_date"]),
its using DateTime
not String
 
Well, at least for the JSON you have sent me
Well, the error you got cannot be reproduced with the JSON. But also, I am not even sure the error was related to that line
 
hmm
 
But the code are just "bad" in general and filled with uncertainty about what fields can be null or not. So I am not surprised if you got a call where json["gmt_last_record_date"] was in fact null
 
2:44 PM
i see
 
I need help, thats for sure
 
It uses a different approach where you are not automatically generating based on JSON content
but where you are setting up your class model and then generate a JSON parser based on that
Also, strongly consider if you need all fields
You are fully allowed to parse a JSON string and just use some of the values
 
See thats the thing, I dont know what fields I will need in the future, so I was grabbing the whole response and creating a model from it and then using what I currently know i need.
I am interested in the code you gave me. Why is the last part of it encapsulated with the ``` symbols?
 
Ah yeah, I just pasted your JSON string into the file. You can also see I added a main method to test it
Just delete that. It just makes the file a Dart program I can run :)
The ''' are used for making a multi-line String in Dart
 
2:49 PM
oh, so i dont need that whole section?
 
github.com/quicktype/quicktype/issues/1673 <- the issue.... the amount of fucking pain these developers have done on the Dart project by not putting a big fat warning about this on their site
@Mackina No no :D
 
ok, check this out
 
I just like to always post Dart code that can be executed standalone so people can try it out by themself
 
Here is where I am trying to use the data from my single device model
 
Use Ctrl+k to format your code here on Stackoverflow chat
 
2:51 PM
currently I am just trying to print out the title of the device
oh ok
LOL
hmm
 
import 'package:flutter/material.dart';
import 'package:gateway_device/backend/api/api_services.dart';
import 'package:gateway_device/models/device_model.dart';

class DeviceProfileWidget extends StatelessWidget {
  final int id;
  const DeviceProfileWidget({Key? key, required this.id}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        body: FutureBuilder(
            future: ApiService().getSingleDevice(id),
 
yes
but all I am getting is the circular indicator
 
But yeah. I can't really help if the problem are part of Flutter since I have never used that.
 
i do see the response being printed in the console
ah ok
 
Well, your problem are rather classic
The future you are sending into FutureBuilder should have been created outside build()
what you are doing now is calling ApiService().getSingleDevice(id) for every frame being drawn on the screen
(potentially)
 
2:56 PM
hmm
i see
oh, no, that is working on my main page
At least I think if I am understanding you correctly
 
Well, it is also only the second paragraph in the documentation so it is understandable so many people are ignoring this fact: https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

> The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be resta
:D
 
hmm
 
> A general guideline is to assume that every build method could get called every frame, and to treat omitted calls as an optimization.
 
import 'package:gateway_device/backend/api/api_services.dart';
import 'package:gateway_device/models/devices_list_model.dart';
import 'package:gateway_device/routes/route_helper.dart';
import 'package:get/get.dart';
import '../flutter_flow/flutter_flow_theme.dart';
import 'package:flutter/material.dart';

class HomePageWidget extends StatefulWidget {
  const HomePageWidget({Key? key}) : super(key: key);

  @override
  _HomePageWidgetState createState() => _HomePageWidgetState();
}

class _HomePageWidgetState extends State<HomePageWidget> {
 
> import 'package:get/get.dart';

Infinite sadness
 
2:59 PM
lol
you said to me last time
 
And I will do it every time!
 
I am moving away from using it, which is why I am trying to work through these issues
I appreciate you so much, you have no idea
I am currently only using it for routes, which I will be changing in the near future.
I was using it for my api calls as well, but its so broken i changed to what I currently use.
 
You should override the initState method inside _HomePageWidgetState and do the call to ApiService().getAllDevices() inside here and save the Future<something> as a variable inside the class which your FutureBuilder can then get
I recommend reading the documentation and see the video here: api.flutter.dev/flutter/widgets/FutureBuilder-class.html
There are a complete code example running inside a DartPad which you can also be inspired by
 
hmm ok
 
I am sure you can do it! :)
 
3:05 PM
that video that shows at the link is how I built this home page and my device profile widget (code I sent a little ways up), is it not?
 
Not something I can say for sure. Again, I am not a Flutter developer so I have not really focused on understanding any of this. :D
 
well, you are a great help just the same
I just feel like I am spinning my wheels with this application and its frustrating because I am building it for work. My boss has no idea how to program with Dart and there is no one I work with that I can ask questions of. I really need to be on a team as a junior developer where I can write code and get help/feedback as needed
 
Well, you are not the first in this problematic position. A lot of people in r/FlutterDev Discord server have told similar stories
But well. Don't try parse more JSON that you need. If you are parsing stuff you are not end up using, it is just potential validation errors if the parsing of these unused fields are failing
It just looks stupid to have a support ticket about the app crashing for some field not even used :D
 
yeah, agreed
what did you use to parse that model file?
hmm I am still getting the null value on the DateTime line
and I know I will be using that field for sure
The frustrating part is that I can see that it is not null in the response from the API
 
@Mackina I just failed and fixed, failed and fixed
@Mackina Well, you can change your factory constructor so it prints the JSON
but again, the whole design are flawed in this parser.... start from the beginning with code not made with that broken generator
 
3:17 PM
I dont know how to build a model this complex, line by line.
 
Read the article I sent you. It should not take long
 
Especially the DateTime fields
ok
 
DateTime should be easy to handle
But well, I think I am done here. Good luck
 
Thank you for your time!
 

last day (15 days later) »