Home > front end >  getting string is not subtype of RxString in getx flutter
getting string is not subtype of RxString in getx flutter

Time:10-09

I am building a simple quote app but when I click on getQuote button it gives me following error.

  • Unhandled Exception: type 'String' is not a subtype of type 'RxString*

code

...rest code
 child: Column(
            children: [
              Obx(
                () => Text(
                  quoteController.quote.toString(),
                  style: const TextStyle(color: Colors.white, fontSize: 45),
                ),
              ),
              ElevatedButton(
                  onPressed: () {
                    quoteController.getQuote();
                  },
                  child: const Text("Get Quote"))
            ],
          ),

...rest code
import 'dart:convert';

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

class QuoteController extends GetxController {
  RxString quote = "".obs;
  getQuote() async {
    var response = await http.get(Uri.parse("https://api.quotable.io/random"));
    quote = jsonDecode(response.body)['content'] as RxString;
  }
}

CodePudding user response:

try putting it like this

quote.value = jsonDecode(response.body)['content'];
update();

Or you can also do this

quote("${jsonDecode(response.body)['content']}")
  • Related