Closed 2shrestha22 closed 2 months ago
Is it throwing an error just by running it?
Is it throwing an error just by running it?
Yes. If computed is present it throws error.
Found the bug and will patch soon. Also cleaned up the code to show how to use list signal:
import 'package:flutter/material.dart';
import 'package:signals_flutter/signals_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with SignalsAutoDisposeMixin {
final textEditingController = TextEditingController();
late final todo = bindSignal(context, listSignal<Todo>([]));
late final activeTodo = createComputed(
context,
() => todo().where((it) => !it.completed),
);
late final completed = createComputed(
context,
() => todo().where((it) => it.completed),
);
@override
Widget build(BuildContext context) {
// todo.watch(context);
activeTodo.watch(context);
completed.watch(context);
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
bottom: const TabBar(
tabs: [
Tab(child: Text('Active')),
Tab(child: Text('Completed')),
],
),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: TabBarView(
children: [
TodoListView(
todos: activeTodo().toList(),
),
TodoListView(
todos: completed().toList(),
)
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return Dialog(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: textEditingController,
autofocus: true,
maxLines: 4,
maxLength: 100,
decoration: const InputDecoration(
border: InputBorder.none,
hintText: 'Add your todo...',
),
),
const SizedBox(height: 8),
FilledButton(
onPressed: () {
todo.add(
(
1,
title: textEditingController.text,
completed: false
),
);
textEditingController.clear();
},
child: const Text('Save'),
)
],
),
),
);
},
);
},
tooltip: 'Increment',
child: const Icon(Icons.add),
),
),
);
}
}
class TodoListView extends StatelessWidget {
const TodoListView({super.key, required this.todos});
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(todos[index].title),
);
},
);
}
}
typedef Todo = (int, {String title, bool completed});
Getting this error while using
createComputed
.