Home > OS >  Flutter update profile value
Flutter update profile value

Time:11-24

I am from Javascript developer and start developing on flutter for company use. I currently facing an issue about setting profile value to gui.

//profile.dart
import 'package:flutter/material.dart';
import 'package:profile/profile.dart';
import 'package:cs_app/models/user.dart';
import 'package:cs_app/models/cs_data.dart';
import 'package:cs_app/models/profile_data.dart';
import 'package:provider/provider.dart';

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

  @override
  State<AdminPage> createState() => _AdminPageState();
}

profile_value(key) async {
  var value = await profileData.user_profile(key);
  print("rtn: "   value);
// rtn: admin, can get the print value
  return value;
}

class _AdminPageState extends State<AdminPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Center(
      child: Profile(
        imageUrl:
            "https://images.unsplash.com/photo-1598618356794-eb1720430eb4?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80",
        name: profile_value("username"), // run func, get rtn value, render
        website: profile_value("website"),
        designation: profile_value("designation"),
        email: "[email protected]",
        phone_number: "12345456",
      ),
    ));
  }
}
//profile_data.dart
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:cs_app/views/login.dart';
import 'dart:convert';
import 'package:cs_app/models/sharedPref.dart';

class profileData {
  static user_profile(key) async {
    var value = await SharedPref().read("user");
    var decode_value = json.decode(value);
    var key_decode_value = decode_value[key];
    print("key: "   key);
    print("value: "   key_decode_value);
// key: username
// value: admin
    return key_decode_value;
  }
}

In my mindset is when _AdminPageState run, the key will run profile_value(key) to get rtn value. But it keeps return The argument type 'Future' can't be assigned to the parameter type 'String'.

CodePudding user response:

Future<String>profile_value(key) async {
  var value = await profileData.user_profile(key);
  print("rtn: "   value);
// rtn: admin, can get the print value
  return value;
}

Method profile_value is Future<String>, so you need to add await in front of name.

name: await profile_value("username"),

CodePudding user response:

In flutter, if you use async func, it's will return a value the same Promise in JS. You must use await or .then to handle it.

  • Related