I get this error when build the page:
E/flutter ( 6971): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: setState() or markNeedsBuild() called during build.
E/flutter ( 6971): This Obx widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase.
E/flutter ( 6971): The widget on which setState() or markNeedsBuild() was called was:
E/flutter ( 6971): Obx
E/flutter ( 6971): The widget which was currently being built when the offending call was made was:
E/flutter ( 6971): RawGestureDetector-[LabeledGlobalKey#42d00]
the page is:
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:get/get.dart';
import '../controllers/checkout_controller.dart';
class CheckoutView extends GetView<CheckoutController> {
@override
const CheckoutView({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Finalizar Pedido'),
centerTitle: true,
),
body: SingleChildScrollView(
child: GetBuilder<CheckoutController>(builder: (controller) {
if (controller.isLoading) {
// Show loading indicator or placeholder while data is being fetched
return const Center(child: CircularProgressIndicator());
} else {
// Build the checkout UI using the fetched data
final billingAddress = controller.addressesController.billing;
final shippingAddress = controller.addressesController.shipping;
return Column(
children: [
const ListTile(
title: Text(
'Endereço de Cobrança:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
title: Text(
'${billingAddress['address_1']}, ${billingAddress['address_2']}n'
'${billingAddress['city']}, ${billingAddress['state']}n'
'${billingAddress['postcode']}',
),
),
const SizedBox(height: 16),
const ListTile(
title: Text(
'Endereço de entrega:',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
title: Text(
'${shippingAddress['address_1']}, ${shippingAddress['address_2']}n'
'${shippingAddress['city']}, ${shippingAddress['state']}n'
'${shippingAddress['postcode']}',
),
),
const ListTile(
title: Text(
'Pagamento',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
ListView.builder(
shrinkWrap: true,
physics: const BouncingScrollPhysics(),
itemCount: controller.paymentMethods.length,
itemBuilder: (context, index) {
final paymentMethod = controller.paymentMethods[index];
return RadioListTile(
title: Text(paymentMethod.title),
subtitle: Html(data: paymentMethod.description),
value: paymentMethod.id,
groupValue: controller.selectedPaymentMethod,
onChanged: (value) =>
controller.setSelectedPaymentMethod(value),
);
},
),
const ListTile(
title: Text(
'Frete',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
const ListTile(
title: Text('Frete grátis.'),
),
const ListTile(
title: Text(
'Observações',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey,
width: 1.0,
),
borderRadius: BorderRadius.circular(4.0),
),
child: TextFormField(
decoration: const InputDecoration(
labelText: 'Suas observações sobre o pedido',
border: InputBorder
.none, // Remove the default border from the input field
contentPadding: EdgeInsets.symmetric(
horizontal: 10.0, vertical: 8.0),
),
onChanged: (value) => controller.setCustomerNote(value),
),
),
),
ElevatedButton(
onPressed: () => controller.doCheckout(),
child: const Text('Finalizar pedido'),
)
],
);
}
}),
));
}
}
Already tried to comment parts of code;
Already used Obx instead GetBuilder;
I’m not using any Obs vars;
I don’t understand why it gets error!!
2
Answers
and in checkout_controller.dart:
Try this ->
I don’t have all the context, but if it works(which I think it should), then the issue is
GetView
Updates itself automagically whenever your controller gets an update. But if you create anotherGetBuilder
inside it, They both try to update the view at the same time. Then this issue occurs.Again, from what I am seeing this should be the issue. Unless there’s some top-level parent issue you’re facing.