【问题标题】:Unhandled Exception: type 'String' is not a subtype of type 'Future<String>' in type cast未处理的异常:类型“String”不是类型转换中“Future<String>”类型的子类型
【发布时间】:2020-06-18 18:01:26
【问题描述】:

我遇到了这个错误,谁能帮我解决这个错误

static Future<String> get_video_lecture_subject(int schoolId,int classroom) async {

  var body;
   body = jsonEncode({
  "school_id": schoolId,
  "classroom": classroom,
  });

  final response = await http.post(
      'https://b5c4tdo0hd.execute-api.ap-south-1.amazonaws.com/testing/get-video-lecture-subjects',
      headers: {"Content-Type": "application/json"},
      body: body,
  );

   print(response.body.toString());
   return response.body.toString();
 }

我在 getpref() 函数中使用了上述函数

 getpref() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    int classNo = int.parse(prefs.getString("class")[0]);
    int schoolId = prefs.getInt("school_id");

      print("hello");
      response=(await ApiService.get_video_lecture_subject(schoolId, classNo)) as Future<String> ;
      print(response);
  }

【问题讨论】:

  • print(response.toString()) 会导致同样的错误吗?
  • 它打印,但有效,但
  • response=(await ApiService.get_video_lecture_subject(schoolId, classNo)) as Future ; ,
  • 此行导致错误
  • String response=await ApiService.get_video_lecture_subject(schoolId, classNo);

标签: oop flutter dart


【解决方案1】:

表达式:

(await ApiService.get_video_lecture_subject(schoolId, classNo)) as Future<String>

调用你的方法get_video_lecture_subject。这将返回一个Future&lt;String&gt;。 然后你等待那个未来,这会产生一个String

然后您尝试将 String 转换为 Future&lt;String&gt;。那失败了,因为字符串不是未来。

尝试删除as Future&lt;String&gt;

【讨论】:

  • “String”类型的值不能分配给“Future”类型的变量。尝试更改变量的类型,或将右侧类型转换为 'Future'
  • 我已经使用了的回复
  • 未来 响应;
  • 如果response的类型是Futue&lt;String&gt;,那么你不能给它分配一个字符串。那么,你想要做什么呢?
【解决方案2】:

查看这个示例,如果它有效,请告诉我

import 'dart:async';

import 'package:flutter/material.dart';

void main() {
  getPrefs();
  runApp(MyApp());
}

void getPrefs() async {
  String value = await yourfunction();
  print(value);
}

Future<String> yourfunction() {
  final c = new Completer<String>();

  // Below is the sample example you can do your api calling here
  Timer(Duration(seconds: 1), () {
    c.complete("you should see me second");
  });
  // Process your thigs above and send the string via the complete method.
  // in you case it will be c.complete(response.body);

  return c.future;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      body: SafeArea(
          child:
              /* FutureBuilder<String>(
            future: yourfunction(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data);
              }

              return CircularProgressIndicator();
            }), */
              Text('')),
    ));
  }
}

【讨论】:

    【解决方案3】:

    用这个替换该行

          final res = await ApiService.get_video_lecture_subject(schoolId, classNo);
    

    更新:更改变量名称(可能它的类型被初始化为 Future)

    【讨论】:

    • “String”类型的值不能分配给“Future”类型的变量。尝试更改变量的类型,或将右侧类型转换为 'Future'
    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 2020-11-19
    • 2021-10-11
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多