turso_dart 0.1.0 copy "turso_dart: ^0.1.0" to clipboard
turso_dart: ^0.1.0 copied to clipboard

Dart binding for the new-gen Turso

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:turso_dart/isolate.dart';

void main() async {
  final path = await getApplicationSupportDirectory();
  final db = await connect(LocalDbConfig('${path.path}/new.db'));
  final conn = await db.connect();
  await conn.execute(
    "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, name TEXT)",
  );
  await conn.execute(
    "INSERT INTO test (name) VALUES (?1)",
    params: Params.positional(["Alice"]),
  );
  await conn.execute(
    "INSERT INTO test (name) VALUES (:name)",
    params: Params.named({":name": "Bob"}),
  );
  print(await conn.query("SELECT * FROM test"));
  final stmt = await conn.prepare("SELECT * FROM test where id = ?1");
  print(await stmt.query(params: Params.positional([1])));
  final tx = await conn.transaction();
  await (await tx.prepare(
    "INSERT INTO test (name) VALUES (:name)",
  )).execute(params: Params.named({":name": "Charlie"}));
  await tx.commit();
  print(await conn.query("SELECT * FROM test"));
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    const textStyle = TextStyle(fontSize: 25);
    const spacerSmall = SizedBox(height: 10);
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Native Packages')),
        body: SingleChildScrollView(
          child: Container(
            padding: const .all(10),
            child: Column(
              children: [
                const Text(
                  'This calls a native function through FFI that is shipped as source in the package. '
                  'The native code is built as part of the Flutter Runner build.',
                  style: textStyle,
                  textAlign: .center,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}