0

What i am trying to do is i am fetching profile from fetchUser.But i don't know any other way that i can pull http data other than string.

class FollowingUserModel {
  final String id;
  final String author;
  User profile;
  final String profileid;

  FollowingUserModel({this.id, this.profileid, this.author, this.profile}) {  profile = fetchUser() as User; }

  Future<User> fetchUser() async{
    final response = await http.get("$SERVER_IP/api/users/$profileid/?format=json");
    if (response.statusCode == 200) {
      var responseJson = json.decode(response.body);
      return User.fromJSON(responseJson);}}

  factory  FollowingUserModel.fromJSON(Map<String, dynamic> jsonMap) {
    return  FollowingUserModel(
      id: jsonMap['id'] as String,
      author:  jsonMap['author'] as String,
      profileid: jsonMap['profile'] as String,
);}}

Does anybody know how to do it?

umarbeyoglu
  • 390
  • 4
  • 19
  • not really.I don't know what to do as it has too much explanation there.It would be better if you break it down as an answer. @nvoigt – umarbeyoglu Feb 10 '21 at 15:27
  • Well, the point of duplicates is that we do not want to have a single, hand-tailored answer for each variant of the same problem, as it makes it hard to find a solution, since each answer only applies to the exact question asked. Since your question is very broad, I don't know how to narrow down an answer. Do you have anything *specific* that you have problems with? Do you get compiler errors, runtime errors? – nvoigt Feb 10 '21 at 15:31
  • I explained it up there,i will explain it again. I want to fetch my User from my class,but i don't know how to return it as a regular User. I want to assign the fetchUser() value to "profile" correctly,thats what i mean. – umarbeyoglu Feb 10 '21 at 15:34
  • I don't know what to tell you. The two answers you got are correct, to wait for a Future to finish and get it's result, you need to use the `await` keyword as shown. If you want to know *why* you have to do that, you can follow my link above. If you have a problem with doing what the answers says, you need to either edit your question to be more specific about the problem you have with it, or ask a follow up question if you think it's too much for a single question. – nvoigt Feb 10 '21 at 15:37
  • I get what you mean.The thing is that i tried both of them,but they still give errors for me. I call my functions from FollowingUserModel class,not inside state or so because it would be too hard for me instead of this one.Thanks for your help anyway – umarbeyoglu Feb 10 '21 at 15:39
  • Well, why don't you post what you tried and post the errors you got? How are we supposed to help you with errors you don't post here? – nvoigt Feb 10 '21 at 15:40
  • check the comments that i left in the answers please... – umarbeyoglu Feb 10 '21 at 15:41
  • I did. I still have no idea what your code looks like. This is my final comment on this issue: *post your code and the errors that you have*. Preferably as a [mcve]. Otherwise we won't be able to help you. We cannot guess what you did wrong in your code if you don't show us your code. – nvoigt Feb 10 '21 at 15:43
  • question has the code look up please – umarbeyoglu Feb 10 '21 at 15:45
  • say , you want to convert Future to User in the same class ? – Kab Agouda Feb 10 '21 at 15:48
  • yes.That is my problem – umarbeyoglu Feb 10 '21 at 15:49
  • You have not yet understood a core concept of dart, **asynchronous programming**. That is not a bad thing, nobody was born with an understanding of that concept, we all had to learn it at some point. You can read the link I provided, or you can read the links the answers provided, or you can read a good book or other teaching material of your choice. But you have to understand that there are no easy answers here. This is not a misplaced comma we can fix. It won't work the way you try it. You have to take the time and read up on the concept, understand it, and *then* find a solution. – nvoigt Feb 10 '21 at 16:01
  • Thanks for the help. – umarbeyoglu Feb 10 '21 at 16:03

2 Answers2

4

Just create an asynchronous function .

If you have Future<User>, to convert it to User, you need to add await; and to use await, you need to add async:

Future<void> test() async {
  User user = await fetchUser();
}

You can learn more about asynchronous function here

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Kab Agouda
  • 6,309
  • 38
  • 32
0

From where you are calling do this,

User user = await followingUserModel.fetchUser();

Future<User> means it will return an User object by asynchronously(in future), so make the call with await, then your code will wait till it get the return.

shuvo
  • 705
  • 5
  • 15
  • I need to call it inside the class model that i am using.Can you instruct me more please? – umarbeyoglu Feb 10 '21 at 15:22
  • where are you calling fetchUser() function? Then why are you fetching data inside a model class? separate the responsibility of the classes. – shuvo Feb 11 '21 at 16:13