skip to Main Content

i’m developing an App with two pages.i use getx and my problem is obx is not refreshed after login eventhough i called the function on initstate and declared the variable as .obs,search is not working and everything is get freezed.When i refresh the page everything is working fine.

This is my UI

`class LandingPage extends StatefulWidget {
  const LandingPage({super.key});

  @override
  State<LandingPage> createState() => _LandingPageState();
}

class _LandingPageState extends State<LandingPage> {
  TextEditingController dateInput = TextEditingController();
  final scaffoldKey = GlobalKey<ScaffoldState>();
  late Future futureTasks;
  String? name;
  String? email;
  String? image;
  var isselected = false.obs;
  @override
  void initState() {
    controller.fetchTodo();
    super.initState();
  }

  final controller = Get.put(ToDoController());
  final usercontroller = Get.put(UserController());
  @override
  Widget build(BuildContext context) {
     return Obx(
      (() {
        return Column(
              children: controller.todo.value.where((element) {
            if (controller.search.value != '') {
              return element.title!
                      .toString()
                      .toLowerCase()
                      .contains(controller.search.value)
            }
            return true;
          }).map((todo) {
            return Stack(
              children: [
                Card(
                  child: ExpansionTile(
                    initiallyExpanded: false,
                    subtitle: Padding(
                      padding: const EdgeInsets.fromLTRB(7, 3, 0, 6),
                      child: Wrap(
                          spacing: 8,
                          runSpacing: 8,
                          children: todo.labels!.map((lable) {
                            return ChipMv(lable: lable);
                          }).toList()),
                    ),
                    title: Align(
                      alignment: Alignment.topLeft,
                      child: TextButton(
                        child: Text(
                            GetUtils.capitalizeFirst(todo.title ?? "")
                                .toString(),
                            style:
                                Theme.of(context).primaryTextTheme.headline5),
                        
                      ),
                    ),
                    children: [
                      Align(
                        alignment: Alignment.topLeft,
                        child: Container(
                          child: ListTile(
                            title: Form(
                              child: TextFormField(
                                controller: controller.task,
                                decoration: InputDecoration(
                                  suffixIcon: IconButton(
                                      icon: Icon(Icons.add),
                                      onPressed: () {
                                        controller.taskCreate(todo.id);
                                      }),
                                  hintText: " Add Task",
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                      Column(
                        children: todo.tasks!
                            .map((task) => Align(
                                  alignment: Alignment.topLeft,
                                  child: Container(
                                    child: CheckboxListTile(
                                        checkboxShape: CircleBorder(),
                                        onChanged: (value) {
                                          task.complete = value;
                                          controller.todoStatus(
                                              todo.id, task.tid, task.complete);
                                        },
                                        value: task.complete,
                                        controlAffinity:
                                            ListTileControlAffinity.trailing,
                                        secondary: IconButton(
                                          icon: Icon(Icons.delete_outlined),
                                          color:
                                              Colors.redAccent.withOpacity(0.6),
                                          onPressed: () {
                                            Get.defaultDialog(
                                              content: Text(
                                                  "Are you want to remove task?"),
                                              actions: <Widget>[
                                                TextButton(
                                                  onPressed: () {
                                                    Get.back();
                                                  },
                                                  child: const Text('Cancel'),
                                                ),
                                                TextButton(
                                                  onPressed: () {
                                                    controller.taskDelete(
                                                        todo.id, task.tid);
                                                    setState(() {});
                                                  },
                                                  child: const Text('OK'),
                                                ),
                                              ],
                                            );
                                          },
                                        ),
                                        title: Text(
                                          task.task ?? "",))
                            .toList(),
                      ),
                    ],                            ],
                          );
                        }
                      },
                    ),
                  ),
                ),
              ],
            );
          }).toList())
        ]);
      }),
    );

     }
    }

This my controller class

`Future<List<Todo>> fetchTodo() async {
    isloading.value = true;
    Uri url = Uri.parse('$dev/todo/todo/list');

    SharedPreferences preferences = await SharedPreferences.getInstance();

    var token = preferences.getString("token");
    print(token);

    // Starting Web API Call.
    var response = await http.post(
      url,
      headers: <String, String>{
        'Content-Type': 'application/json; charset=UTF-8',
        'Authorization': 'Bearer $token',
      },
    );.
    if (response.statusCode == 200) {
      final datas = json.decode(response.body).cast<Map<String, dynamic>>();
      tasks = datas.map<Todo>((json) {
        return Todo.fromJson(json);
      }).toList();
      todo.value = datas.map<Todo>((json) {
        return Todo.fromJson(json);
      }).toList();
      isloading.value = false;
      return todo;
    } else {
      isloading.value = false;
      return todo;
    }
  }

`

2

Answers


  1. Chosen as BEST ANSWER
      import 'dart:html' as html;
    
     Future Login() {
    Uri url = Uri.parse('url here');
    
    // Starting Web API Call.
    var response = await http.post(url,
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
        },
        body: body);
    
    if (response.statusCode == 200) {
      html.window.location.reload();
    } else {
      Get.snackbar("Failed!", "Username and Password not found");
    }
    

    it reloads the page after login and everything works fine.


  2. instead of returning todo like that,
    try to do like this:

    in controller declare a RxList:

    RxList<Todo> todoList = <Todo>[].obs;
    
    

    and change the fetchTodo like this:

    Future<void>fetchTodo() async {
    
      //some codes to receive todoList
      todoList.value = something; // todoList you received from above
      todoList.refresh();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search