shared_preferences 0.1.1 copy "shared_preferences: ^0.1.1" to clipboard
shared_preferences: ^0.1.1 copied to clipboard

outdatedDart 1 only

A Flutter plugin for reading and writing simple key-value pairs

example/lib/main.dart

// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'SharedPreferences Demo',
      home: new SharedPreferencesDemo(),
    );
  }
}

class SharedPreferencesDemo extends StatefulWidget {
  SharedPreferencesDemo({ Key key }) : super(key: key);

  @override
  SharedPreferencesDemoState createState() => new SharedPreferencesDemoState();
}

class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  Future<Null> _incrementCounter() async {
    SharedPreferences prefs = await _prefs;
    int counter = (prefs.getInt('counter') ?? 0) + 1;
    setState(() {
      prefs.setInt("counter", counter);
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: const Text("SharedPreferences Demo"),
      ),
      body: new Center(
        child: new FutureBuilder(
          future: _prefs,
          builder: (BuildContext context, AsyncSnapshot<SharedPreferences> snapshot) {
            if (snapshot.connectionState == ConnectionState.waiting)
              return const Text('Loading...');
            int counter = snapshot.requireData.getInt('counter') ?? 0;
            return new Text(
              'Button tapped $counter time${ counter == 1 ? '' : 's' }.\n\n'
              'This should persist across restarts.',
            );
          }
        )
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ),
    );
  }
}
9751
likes
0
pub points
100%
popularity

Publisher

verified publisherflutter.dev

A Flutter plugin for reading and writing simple key-value pairs

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on shared_preferences