Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

type 'Rx<Text>' is not a subtype of type 'Widget' in type cast<…>

I want to make the Widget obserable in flutter when using get get: ^4.3.8, the controller code like this:

class MainController extends GetxController {
  Widget childWidget = Text("Loading...").obs as Widget;
}

and the minimal reproduce main.dart look like this:

import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/get_state.dart';

import 'main_controller.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
        init: MainController(),
        builder: (controller) {
          return Scaffold(
            //appBar: AppBar(title: Text("title")),
            body: Text(""),
          );
        });
  }
}

when compile the app, shows error like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   flutter: \^[[38;5;196m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞════════════════════════<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196mThe following _CastError was thrown building Quadrant(dirty,<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196mdependencies: [MediaQuery]):<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196mtype 'Rx<Text>' is not a subtype of type 'Widget' in type cast<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196mThe relevant error-causing widget was:<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m  Quadrant<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m  Quadrant:file:///Users/xiaoqiangjiang/source/reddwarf/frontend/tik/lib/navigators/nav/nav_page.dart:31:31<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196mWhen the exception was thrown, this was the stack:<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m#0      new QuadrantController (package:Tik/pages/quadrant/quadrant_controller.dart:14:47)<…>
    flutter: \^[[38;5;196m│ \^[[0m\^[[39m\^[[48;5;196m#1      Quadrant.build (package:Tik/pages/quadrant/quadrant.dart:14:15)<…>

is it possible to make widget component obs when using get? why shows the Text not the subtype of wiget? I think the Text is the subtype of Widget.

>Solution :

You can try this like :

 Rx<Widget> childWidget = Text("Loading...").obs;

and to use this obs widget in widget tree like :

GetBuilder<MainController>(
        init: MainController(),
        builder: (controller) {
          return Scaffold(
            body: controller.childWidget.value,
          );
        });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading